Dynamic Lang. Runtime vs Reflection

前端 未结 6 995
醉梦人生
醉梦人生 2020-11-29 21:47

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.

6条回答
  •  一向
    一向 (楼主)
    2020-11-29 22:13

    The way I see it all your cons for using dynamic except interoperability with older .NET versions are also present when using Reflection:

    Affects application performance

    While it does affect the performance, so does using Reflection. From what I remember the DLR more or less uses Reflection the first time you access a method/property of your dynamic object for a given type and caches the type/access target pair so that later access is just a lookup in the cache making it faster then Reflection

    Dynamic keyword is internally a wrapper of Reflection

    Even if it was true (see above), how would that be a negative point? Whether or not it does wrap Reflection shouldn't influence your application in any significant matter.

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

    While this is true, as long as you use it sparingly it shouldn't be that much of a problem. Furthermore is you basically use it as a replacement for reflection (that is you use dynamic only for the briefest possible durations when you want to access something via reflection), the risk of such bugs shouldn't be significantly higher then if you use reflection to access your methods/properties (of course if you make everything dynamic it can be more of a problem).

    Affects interoperability with previous .NET versions

    For that you have to decide yourself how much of a concern it is for you.

提交回复
热议问题