问题
What is the difference between Assembly.GetExecutingAssembly()
and typeof(program).Assembly
?
回答1:
Assuming program
is in the executing assembly, they should both return the same value. However, typeof(program).Assembly
should have better performance, since Assembly.GetExecutingAssembly()
does a stack walk. In a micro benchmark on my machine, the former took about 20ns, while the latter was 30x slower at about 600ns.
If you control all the code I think you should always use typeof(program).Assembly
. If you provided source code that other people could build into their assemblies, you would need to use Assembly.GetExecutingAssembly()
.
回答2:
Calling Assembly.GetExecutingAssembly()
will return the assembly containing the method that is calling Assembly.GetExecutingAssembly()
.
Calling for example typeof(string).Assembly
will return mscorlib.dll because it contains the type String
.
On the other hand if you have a project called MyProject and somewhere in this project you call Assembly.GetExecutingAssembly()
it will return the Assembly instance representing MyProject.dll
Hope this clarifies.
回答3:
Assembly.GetExecutingAssembly():
Gets the assembly that contains the code that is currently executing.
The following example gets the assembly of the currently running 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 use in reflection.
The typeof operator is used to obtain the System.Type object for a type. A typeof expression takes the form:
To obtain the run-time type of an expression, you can use the .NET Framework method 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());
}
}
}
Output
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()
回答4:
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 string is defined in mscorlib assembly its full name is returned. So the assembly name will be returned where the type is defined. If I pass my class where I am executing 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"
来源:https://stackoverflow.com/questions/15407340/difference-between-assembly-getexecutingassembly-and-typeofprogram-assembly