【问题标题】:Difference between Assembly.GetExecutingAssembly() and typeof(program).AssemblyAssembly.GetExecutingAssembly() 和 typeof(program).Assembly 之间的区别
【发布时间】:2013-03-14 10:59:08
【问题描述】:

Assembly.GetExecutingAssembly()typeof(program).Assembly 有什么区别?

【问题讨论】:

  • 好吧,如果program 不属于正在执行的程序集,他们会给出不同的答案...(例如,如果您从类库中调用它。)您是否阅读过文档为GetExecutingAssembly?不清楚是什么让您感到困惑。
  • 如果您的问题包含有关您想要做什么的更多信息,那将更有意义。就目前而言,您有点问“橙子和苹果有什么区别?”

标签: c# c#-4.0


【解决方案1】:

假设program 在执行程序集中,它们都应该返回相同的值。但是,typeof(program).Assembly 应该有更好的性能,因为Assembly.GetExecutingAssembly() 会进行堆栈遍历。在我机器上的微基准测试中,前者大约需要 20ns,而后者在大约 600ns 时慢了 30 倍。

如果您控制所有代码,我认为您应该始终使用typeof(program).Assembly。如果您提供了其他人可以构建到他们的程序集中的源代码,则需要使用Assembly.GetExecutingAssembly()

【讨论】:

  • 您知道调用GetExecutingAssembly 的方法是否存在被内联并返回意外程序集的风险吗?就像如果 AssemblyB 中的派生类调用从 AssemblyA 中的基类继承的实例方法,并且该方法是 GetExecutingAssembly 的单行代码,我们能否确定返回了 AssemblyA?
  • 我很想知道您是如何进行微基准测试的!
【解决方案2】:

调用Assembly.GetExecutingAssembly() 将返回包含调用Assembly.GetExecutingAssembly() 的方法的程序集。

例如调用typeof(string).Assembly 将返回mscorlib.dll,因为它包含String 类型。 另一方面,如果您有一个名为 MyProject 的项目,并且在该项目的某处调用 Assembly.GetExecutingAssembly(),它将返回代表 MyProject.dll

的 Assembly 实例

希望这可以澄清。

【讨论】:

    【解决方案3】:

    Assembly.GetExecutingAssembly():
    获取包含当前正在执行的代码的程序集。 以下示例获取当前运行代码的程序集。

    Assembly SampleAssembly;
    // Instantiate a target object.
    Int32 Integer1 = new Int32();
    Type Type1;
    // Set the Type instance to the target class type.
    Type1 = Integer1.GetType();
    // Instantiate an Assembly class to the assembly housing the Integer type.  
    SampleAssembly = Assembly.GetAssembly(Integer1.GetType());
    // Display the name of the assembly currently executing
    Console.WriteLine("GetExecutingAssembly=" + Assembly.GetExecutingAssembly().FullName);
    

    typeOf():
    它主要用于反射。
    typeof 运算符用于获取类型的 System.Type 对象。 typeof 表达式采用以下形式: 要获取表达式的运行时类型,可以使用 .NET Framework 方法 GetType。

    Example
    // cs_operator_typeof.cs
    // Using typeof operator
    using System;
    using System.Reflection;
    
    public class MyClass 
    {
       public int intI;
       public void MyMeth() 
       {
       }
    
       public static void Main() 
       {
          Type t = typeof(MyClass);
    
          // alternatively, you could use
          // MyClass t1 = new MyClass();
          // Type t = t1.GetType();
    
          MethodInfo[] x = t.GetMethods();
          foreach (MethodInfo xtemp in x) 
          {
             Console.WriteLine(xtemp.ToString());
          }
    
          Console.WriteLine();
    
          MemberInfo[] x2 = t.GetMembers();
          foreach (MemberInfo xtemp2 in x2) 
          {
             Console.WriteLine(xtemp2.ToString());
          }
       }
    }
    

    输出

    Int32 GetHashCode()
    Boolean Equals(System.Object)
    System.String ToString()
    Void MyMeth()
    Void Main()
    System.Type GetType()
    
    Int32 intI
    Int32 GetHashCode()
    Boolean Equals(System.Object)
    System.String ToString()
    Void MyMeth()
    Void Main()
    System.Type GetType()
    Void .ctor()
    

    【讨论】:

    • 我看到您使用的第一个代码示例直接来自 MSDN 上的文档,但我仍然认为这是一个糟糕且编写不佳的示例。
    【解决方案4】:

    以下代码解释了差异。

    //string class is defined by .NET framework
    var a = Assembly.GetAssembly(typeof(string));
    //a = FullName = "mscorlib, Version=4.0.0.0, 
    Culture=neutral, PublicKeyToken=b77a5c561934e089"
    

    由于字符串是在 mscorlib 程序集中定义的,它的全名被返回。所以程序集名称将在定义类型的地方返回。如果我通过我正在执行此代码的课程,

    //Program is the class where this code is being executed
    var aa = Assembly.GetAssembly(typeof(Program)); 
    //aa = FullName = "Obj_2_5_Using_Reflection, 
    Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
    
    var b = Assembly.GetExecutingAssembly();
    //b = FullName = "Obj_2_5_Using_Reflection, 
    Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
    

    【讨论】:

      猜你喜欢
      • 2021-12-21
      • 1970-01-01
      • 2013-01-28
      • 1970-01-01
      • 2020-07-28
      • 2012-12-17
      • 2016-02-12
      • 2016-11-27
      • 1970-01-01
      相关资源
      最近更新 更多