C# Assembly.GetExecutingAssembly() 和 typeof(program).Assembly 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15407340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between Assembly.GetExecutingAssembly() and typeof(program).Assembly
提问by Ravi Karthik
What is the difference between Assembly.GetExecutingAssembly()
and typeof(program).Assembly
?
Assembly.GetExecutingAssembly()
和 和有typeof(program).Assembly
什么区别?
回答by Arturo Martinez
Calling Assembly.GetExecutingAssembly()
will return the assembly containing the method that is calling Assembly.GetExecutingAssembly()
.
调用Assembly.GetExecutingAssembly()
将返回包含调用方法的程序集Assembly.GetExecutingAssembly()
。
Calling for example typeof(string).Assembly
will return mscorlib.dllbecause it contains the type String
.
On the other hand if you have a project called MyProjectand somewhere in this project you call Assembly.GetExecutingAssembly()
it will return the Assembly instance representing MyProject.dll
例如,调用typeof(string).Assembly
将返回mscorlib.dll,因为它包含类型String
。另一方面,如果您有一个名为MyProject的项目,并且在该项目中的某处调用Assembly.GetExecutingAssembly()
它将返回代表MyProject.dll的 Assembly 实例
Hope this clarifies.
希望这能澄清。
回答by Shreyos Adikari
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.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():
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.
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());
}
}
}
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()
回答by Mike Harder
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.
假设program
在执行程序集中,它们都应该返回相同的值。但是,typeof(program).Assembly
应该有更好的性能,因为Assembly.GetExecutingAssembly()
会进行堆栈遍历。在我机器上的微基准测试中,前者大约需要 20ns,而后者在大约 600ns 时慢了 30 倍。
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()
.
如果您控制所有代码,我认为您应该始终使用typeof(program).Assembly
. 如果您提供了其他人可以构建到其程序集中的源代码,则需要使用Assembly.GetExecutingAssembly()
.
回答by RotatingWheel
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,
由于字符串是在 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"