简体   繁体   English

C#:如何在运行时向对象添加属性?

[英]C#: How to add an attributes to an object at run-time?

作为一个实体类,我想在运行时添加一个属性,我该怎么办?

What needs to see the attributes? 需要看什么属性? If it is things like data-binding etc, TypeDescriptor should work: 如果它是数据绑定等, TypeDescriptor应该工作:

TypeDescriptor.AddAttributes(type, attribs);
TypeDescriptor.AddAttributes(instance, attribs);

This only affects System.ComponentModel usage (not direct reflection), but that is often enough - for example, you can associate a TypeConverter via the above. 这只会影响System.ComponentModel使用(不是直接反射),但这通常就足够了 - 例如,您可以通过上面的方式关联TypeConverter

If by "attributes" you mean "properties", then (again, as far as data-binding is concerned) TypeDescriptor also has potential there - but it is non-trivial; 如果“属性”你的意思是“属性”,那么(同样,就数据绑定而言) TypeDescriptor也有潜力 - 但它是非平凡的; you need to either implement ICustomTypeDescriptor on the object, or to write a CustomTypeDescriptor for the type - and in either case, you need to write your own PropertyDescriptor implementation (often talking to a per-instance dictionary etc). 您需要在对象上实现ICustomTypeDescriptor ,或者为类型编写CustomTypeDescriptor - 在任何一种情况下,您都需要编写自己的PropertyDescriptor实现(通常与每个实例字典交谈等)。 This will get used by anything that uses: 这将被使用的任何东西使用:

// only works if you use TypeDescriptionProvider
PropertyDescriptorCollection typeProps = TypeDescriptor.GetProperties(type);
// works via TypeDescriptionProvider or ICustomTypeDescriptor
PropertyDescriptorCollection objProps = TypeDescriptor.GetProperties(obj);

Again, this covers a wide range of data-binding and similar scenarios. 同样,这涵盖了广泛的数据绑定和类似场景。 For an example of this, see here - it is far from trivial, however. 举个例子, 请看这里 - 然而,这远非微不足道。 The example usage (from the link) adds two properties at runtime: 示例用法(来自链接)在运行时添加两个属性:

Bag.AddProperty<int>("TestProp", new DefaultValueAttribute(5)); 
Bag.AddProperty<string>("Name"); 

Edit: please clarify, are you talking about c# attributes or members on your class? 编辑:请澄清,你是在谈论c#属性还是你班上的成员?

The only way you can add c# attributes is to generate a completely new class with the additional attributes, compile and load the new assembly to your existing AppDomain. 添加c#属性的唯一方法是使用其他属性生成一个全新的类,编译并将新程序集加载到现有的AppDomain。

Use a hashtable to store your attributes. 使用哈希表来存储您的属性。

If you want more runtime flexibility, you might try Ruby or some other interpreted language. 如果您想要更多的运行时灵活性,可以尝试Ruby或其他一些解释语言。

Take a look at the Dynamic Language Runtime. 看看动态语言运行时。 You also might consider a dynamic language like IronRuby or IronPython. 您也可以考虑使用IronRuby或IronPython等动态语言。

What problam are you trying to solve? 你试图解决什么问题?

http://en.wikipedia.org/wiki/Dynamic_Language_Runtime http://en.wikipedia.org/wiki/Dynamic_Language_Runtime

This post answered this for me: 这篇文章为我解答了这个问题:

Dynamically Creating Types at Runtime 在运行时动态创建类型

Attributes are part of the meta-data of a type and so they are hardcoded in the compiled assembly (that's also why you are only allowed to use some primitive types and not arbitrary data at attributes). 属性是类型的元数据的一部分,因此它们在编译的程序集中被硬编码(这也是为什么只允许使用某些基本类型而不是属性的任意数据)。

The consequence is that you can't add any attributes to a type at runtime. 结果是您无法在运行时向类型添加任何属性。 But there are various alternative techniques. 但是有各种替代技术。 You could use simple dictionaries or something more powerful like attached dependency properties . 您可以使用简单的词典或更强大的功能,如附加的依赖项属性

I would go with PostSharp , a very elegant AOP framework (or Policy Injection ). 我会选择PostSharp ,一个非常优雅的AOP框架 (或Policy Injection )。

PostSharp allows you to inject custom attributes . PostSharp 允许您注入自定义属性
The blog entry refered to in the post has some code you can download to achieve your goal. 在帖子中提到的博客条目有一些代码可以下载以实现您的目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM