Difference between Assembly.GetExecutingAssembly () and typeof (program).

What is the difference between Assembly.GetExecutingAssembly() and typeof(program).Assembly ?

+7
source share
4 answers

Assuming program is in the executable assembly, they should return the same value. However, typeof(program).Assembly should have better performance since Assembly.GetExecutingAssembly() executes the stack. In the micro benchmark on my machine, the first one took about 20 ns, and the last one was 30 times slower than about 600 ns.

If you control all the code, I think you should always use typeof(program).Assembly . If you provided source code that other people could embed in their assemblies, you would need to use Assembly.GetExecutingAssembly() .

+8
source

A call to Assembly.GetExecutingAssembly() will return an assembly containing the method that calls Assembly.GetExecutingAssembly() .

A call, such as typeof(string).Assembly , will return mscorlib.dll because it contains a type of String . On the other hand, if you have a project called MyProject and somewhere in this project you call Assembly.GetExecutingAssembly() , it will return an assembly instance representing MyProject.dll

Hope this clarifies.

+3
source

Assembly.GetExecutingAssembly ():
Gets the assembly that contains the code that is currently executing. The following example gets the assembly of the current code.

 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 ():
It is mainly used in reflection.
The typeof operator is used to get the System.Type object for the type. The typeof expression takes the form: To get the type of the expression at run time, you can use the GetType.NET Framework method.

 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()); } } } 

Exit

 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() 
+1
source

The following code explains the differences.

 //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" 

Since the string is defined in the mscorlib assembly, its full name is returned. Thus, the assembly name will be returned where the type is specified. If I pass my class where I execute this code,

 //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" 
0
source

All Articles