Assembly.GetExecutingAssembly()和typeof(program).Assembly之间的区别 [英] Difference between Assembly.GetExecutingAssembly() and typeof(program).Assembly
问题描述
Assembly.GetExecutingAssembly()
和 typeof(program).Assembly
有什么区别?
假定程序
在执行的程序集中,它们都应返回相同的值。但是, typeof(program).Assembly
应该有更好的性能,因为 Assembly.GetExecutingAssembly()
可以进行堆栈遍历。在我的计算机上的微型基准测试中,前者花费了大约20ns,而后者则慢了30倍,达到了约600ns。使用 typeof(program).Assembly
。如果您提供了其他人可以内置到其程序集中的源代码,则需要使用 Assembly.GetExecutingAssembly()
。
What is the difference between Assembly.GetExecutingAssembly()
and typeof(program).Assembly
?
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()
.
这篇关于Assembly.GetExecutingAssembly()和typeof(program).Assembly之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!