Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
110 views
in Technique[技术] by (71.8m points)

c# - Dynamic Lang. Runtime vs Reflection

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:

  • ReadableMaintainable code.
  • Fewer lines of code.

While the negatives associated with using dynamic keyword, I came to hear was like:

  • Affects application performance.
  • Dynamic keyword is internally a wrapper of Reflection.
  • Dynamic typing might turn into breeding ground for hard to find bugs.
  • Affects interoperability with previous .NET versions.

Please help me on whether the pros and cons I came across are sensible or not?

question from:https://stackoverflow.com/questions/4646786/dynamic-lang-runtime-vs-reflection

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

ReadableMaintainable code

Certainly true in my experence.

Fewer lines of code.

Not significantly, but it will help.

Affects application performance.

Very slightly. But not even close to the way reflection does.

Dynamic keyword is internally a wrapper of Reflection.

Completely untrue. The dynamic keyword leverages the Dynamic Library Runtime.

[Edit: correction as per comment below]

It would seem that the Dynamic Language Runtime does use Reflection and the performance improvements are only due to cacheing techniques.

Dynamic typing might turn into breeding ground for hard to find bugs.

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.

Affects interoperability with previous .NET versions

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.

One significant pro that you're missing is the improved interoperability with COM/ATL components.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...