什么是"成本"的.NET反射? [英] What is the "cost" of .NET reflection?

查看:193
本文介绍了什么是"成本"的.NET反射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  如何昂贵是.NET反射?

目前我在编程的心态,反思是我最好的朋友。我用了很多的内容动态加载,允许宽松执行,而不是严格的接口,以及大量的自定义属性。

I am currently in a programming mentality that reflection is my best friend. I use it a lot for dynamic loading of content that allows "loose implementation" rather than strict interfaces, as well as a lot of custom attributes.

什么是真实成本使用反射?

What is the "real" cost to using reflection?

是否值得对经常反映类型的努力,已缓存的反思,比如我们自己的pre-LINQ DAL对象$ C $的所有属性表定义?

Is it worth the effort for frequently reflected types to have cached reflection, such as our own pre-LINQ DAL object code on all the properties to table definitions?

请问outwieght的反映CPU的使用缓存内存占用?

Would the caching memory footprint outwieght the reflection CPU usage?

推荐答案

反思需要大量的类型的元数据被加载,然后进行处理。这可能导致更大的存储器开销和较慢的执行速度。根据这篇文章属性修改为约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.

这篇关于什么是"成本"的.NET反射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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