且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

什么是"成本"的.NET反射?

更新时间:2022-06-10 08:32:45

反思需要大量的类型的元数据被加载,然后进行处理。这可能导致更大的存储器开销和较慢的执行速度。根据这篇文章属性修改为约2.5倍,3倍速度较慢,方法调用是3.5x- 4倍速度较慢。

Reflection requires a large amount of the type metadata to be loaded and then processed. This can result in a larger memory overhead and slower execution. According to this article property modification is about 2.5x-3x slower and method invocation is 3.5x-4x slower.

下面是一个很好的 MSDN文章概述如何使反射更快,那里的开销。我强烈建议你阅读,如果你想了解更多信息。

Here is an excellent MSDN article outlining how to make reflection faster and where the overhead is. I highly recommend reading if you want to learn more.

还有复杂的元件,其反射可以添加到code,使得它基本上更加混乱,从而难以处理。有些人,如在Scott Hanselman 相信,通过使用反射,你常常使比你解决更多的问题。这是特别的情况下,如果你的团队主要是初级开发者。

There is also an element of complexity that reflection can add to the code that makes it substantially more confusing and hence difficult to work with. Some people, like Scott Hanselman believe that by using reflection you often make more problems than you solve. This is especially the case if your teams is mostly junior devs.

您可能会更好寻找到DLR(动态语言运行时),如果你需要很多的动态行为。随着即将在.NET 4.0中的新变化,你可能想看看你是否可以将某些入您的解决方案。动态使用动态code非常优雅和创建自己的动态对象相当直截了当增加的支持VB和C#做。

You may be better off looking into the DLR (Dynamic Language Runtime) if you need alot of dynamic behaviour. With the new changes coming in .NET 4.0 you may want to see if you can incorporate some of it into your solution. The added support for dynamic from VB and C# make using dynamic code very elegant and creating your own dynamic objects fairly straight forward.

祝你好运。

编辑:我做了一些更多的围绕斯科特的网站戳,发现在思考这个播客。我还没有听过它,但它可能是值得的。

I did some more poking around Scott's site and found this podcast on reflection. I have not listened to it but it might be worth while.