问题描述
我打算在我的新项目中使用 dynamic 关键字.但在介入之前,我想知道使用动态关键字而不是反射的优缺点.
I am planning to use dynamic keyword for my new project. But before stepping in, I would like to know about the pros and cons in using dynamic keyword over Reflection.
按照专业人士的说法,我可以找到有关动态关键字的信息:
Following where the pros, I could find in respect to dynamic keyword:
- 可读可维护代码.
- 更少的代码行.
虽然与使用动态关键字相关的负面影响,但我听到的是:
While the negatives associated with using dynamic keyword, I came to hear was like:
- 影响应用程序性能.
- Dynamic 关键字在内部是 Reflection 的包装器.
- 动态类型可能会成为难以发现错误的温床.
- 影响与以前的 .NET 版本的互操作性.
请帮我看看我遇到的利弊是否合理?
Please help me on whether the pros and cons I came across are sensible or not?
推荐答案
根据我的经验当然是正确的.
Certainly true in my experence.
更少的代码行.
不显着,但会有所帮助.
Not significantly, but it will help.
影响应用程序性能.
非常轻微.但与反射的方式相差甚远.
Very slightly. But not even close to the way reflection does.
Dynamic 关键字在内部是 Reflection 的包装器.
似乎动态语言运行时确实使用了反射,而性能改进仅归功于缓存技术.
It would seem that the Dynamic Language Runtime does use Reflection and the performance improvements are only due to cacheing techniques.
动态类型可能会成为难以发现错误的温床.
这可能是真的;这取决于您如何编写代码.您正在有效地从代码中删除编译器检查.如果您的测试覆盖率很好,这可能无关紧要;如果没有,那么我怀疑你会遇到问题.
This may be true; it depends how you write your code. You are effectively removing compiler checking from your code. If your test coverage is good, this probably won't matter; if not then I suspect you will run into problems.
影响与以前的 .NET 版本的互操作性
不是真的.我的意思是您将无法针对旧版本编译代码,但是如果您想这样做,那么您应该使用旧版本作为基础并对其进行向上编译,而不是相反.但是,如果您想使用 .NET 2 库,那么您应该不会遇到太多问题,只要您在 app.config/web.config 中包含声明即可.
Not true. I mean you won't be able to compile your code against older versions, but if you want to do that then you should use the old versions as a base and up-compile it rather than the other way around. But if you want to use a .NET 2 library then you shouldn't run into too many problems, as long as you include the declaration in app.config / web.config.
您缺少的一个重要优点是改进了与 COM/ATL 组件的互操作性.
One significant pro that you're missing is the improved interoperability with COM/ATL components.
这篇关于动态朗.运行时与反射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!