.NET 反射的代价有多大? [英] How costly is .NET reflection?

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

问题描述

我经常听说使用反射有多糟糕.虽然我通常会避免反思并且很少发现没有它就无法解决我的问题的情况,但我想知道......

I constantly hear how bad reflection is to use. While I generally avoid reflection and rarely find situations where it is impossible to solve my problem without it, I was wondering...

对于那些在应用程序中使用过反射的人,你测量过性能影响吗,它真的那么糟糕吗?

For those who have used reflection in applications, have you measured performance hits and, is it really so bad?

推荐答案

是的.但这取决于您要尝试做什么.

It is. But that depends on what you're trying to do.

我使用反射来动态加载程序集(插件),它的性能惩罚"不是问题,因为操作是我在应用程序启动期间所做的.

I use reflection to dynamically load assemblies (plugins) and its performance "penalty" is not a problem, since the operation is something I do during startup of the application.

但是,如果您在一系列嵌套循环中进行反射,每个循环都有反射调用,我会说您应该重新访问您的代码 :)

However, if you're reflecting inside a series of nested loops with reflection calls on each, I'd say you should revisit your code :)

对于几次"操作,反射是完全可以接受的,您不会注意到它有任何延迟或问题.这是一个非常强大的机制,它甚至被 .NET 使用,所以我不明白为什么你不应该尝试一下.

For "a couple of time" operations, reflection is perfectly acceptable and you won't notice any delay or problem with it. It's a very powerful mechanism and it is even used by .NET, so I don't see why you shouldn't give it a try.

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

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