是否有一个很好的理由,preferring反射比的参考? [英] Is there a good reason for preferring reflection over reference?

查看:163
本文介绍了是否有一个很好的理由,preferring反射比的参考?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在看一些旧的code,我遇到了一件code,它是使用反射加载一些dll的,他们的来源$ C ​​$ C是可利用的(他们是在解决方案中的另一个项目)。

Going over some legacy code, I ran into piece of code that was using reflection for loading some dll's that their source code was available (they were another project in the solution).

我开裂我的头骨试图弄清楚它为什么做这样(自然是code没有记录......)。

I was cracking my skull trying to figure out why it was done this way (naturally the code was not documented...).

我的问题是,你能想到的任何好的理由preferring加载通过反射程序集,而不是引用它?

My question is, can you think about any good reason for preferring to load an assembly via reflection rather than referencing it?

推荐答案

是的,如果你有一个动态模块系统,不同的DLL应视在运行条件下加载。我们这样做,我的工作;我们做了可以被装载到我们的系统中,然后只装载与每个模块相关联将DLL如果许可证检查出不同的可选模块的许可证检查。这不应该被加载执行此prevents code,它可以同时提高性能稍微prevent错误。

Yes, if you have a dynamic module system, where different DLLs should be loaded depending on conditions at runtime. We do this where I work; we do a license check for different optional modules that may be loaded into our system, and then only load the DLLs associated with each module if the license checks out. This prevents code that should never be executed from being loaded, which can both improve performance slightly and prevent bugs.

动态加载的DLL也可以让你彻底改变功能,而无需改变任何来源$ C ​​$ C。主组件可例如在运动设定一个发现过程,其中它发现实现一些接口的所有类,并选择根据某些运行时准则来使用哪一个。

Dynamically loading DLLs may also allow you to drastically change functionality without changing any source code. The main assembly may for instance set in motion a discovery process where it finds all classes that implement some interface, and chooses which one to use depending on some runtime criterion.

您通常需要使用 MEF 为此这些天样的任务,但因为.NET 4.0,这只是被周围,所以有可能很多codebases在那里,做手工。 (我不知道很多关于MEF,也许你需要做的这部分手动那里。)

These days you'll typically want to use MEF for this kind of task, but that's only been around since .NET 4.0, so there are probably many codebases out there that do it manually. (I don't know much about MEF. Maybe you have to do this part manually there as well.)

但无论如何,在回答你的问题是,当然有充分的理由来动态加载使用反射的DLL。是否适用于你的情况是不可能的,没有更多的细节说。

But anyway, the answer to your question is that there certainly are good reasons to dynamically load DLLs using reflection. Whether it applies in your case is impossible to say without more details.

这篇关于是否有一个很好的理由,preferring反射比的参考?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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