在C#4.0运行时附加属性和方法? [英] Attaching properties and methods at runtime in C# 4.0?

查看:129
本文介绍了在C#4.0运行时附加属性和方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在运行时使用动态功能附加方法和属性的性能意义如下:这个博文

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很漂亮。我的个人担心不如缺乏类型安全性 - 打字错误可以很容易地将您的代码锁定在编译器上。

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中的动态打字,它给出了非常明显的好处...我期望相对较少(如不安全的代码)。

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 ?不是那么多,IMO - 主要是在和其他动态语言交谈时。

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

这篇关于在C#4.0运行时附加属性和方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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