在运行时在C#4.0附加的属性和方法?属性、方法

2023-09-03 16:58:48 作者:不渡

什么是使用动态功能连接方法和属性在运行时对性能的影响,如这说明博客文章?

What are the performance implications of attaching methods and properties at run time using dynamic features, as described in this blog post?

什么是ExpandoObject的实际用途?

What are the practical uses of ExpandoObject?

推荐答案

好了,你不是的真正的安装方法和属性的对象。不至于CLR而言。你只是将条目添加到字典,当 ExpandoObject 实施 IDynamicMetaObjectProvider 被要求的属性值(或要求执行方法),它会采取相应的行动。

Well, you're not really attaching methods and properties to the object. Not as far as the CLR is concerned. You're just adding entries into a dictionary, and when the ExpandoObject implementation of IDynamicMetaObjectProvider is asked for the property value (or asked to execute the method) it will act accordingly.

性能明显会比静态绑定的访问方法/属性慢,但DLR是pretty的刺骨。我个人担心是不是表现这么多,因为缺乏类型安全的 - 一个错字可以很容易地拧你的code时没有编译器察觉它

Performance is obviously going to be slower than statically bound access to methods/properties, but the DLR is pretty nippy. My personal worry isn't performance so much as the lack of type safety - a typo can easily screw your code up without the compiler spotting it.

我个人的建议是,只使用动态类型在C#4,在给出了一个非常明确的利益......我希望是比较少见的(比如不安全code)。

My personal advice is to only use dynamic typing in C# 4 where it gives a very clear benefit... which I expect to be relatively rare (like unsafe code).

使用的 ExpandoObject ?不是很多,国际海事组织 - 主要是交谈时,其他动态语言

Uses for ExpandoObject? Not that many, IMO - mostly when talking to other dynamic languages.