Assembly.GetExecutingAssembly()和typeof(program).Assembly之间的区别 [英] Difference between Assembly.GetExecutingAssembly() and typeof(program).Assembly

查看:71
本文介绍了Assembly.GetExecutingAssembly()和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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆