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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/563548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 08:19:03  来源:igfitidea点击:

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

c#.netattributes

提问by user68145

As an entity class, I want to add an attributes at run-time, how should I do?

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

回答by Rex M

Edit:please clarify, are you talking about c# attributesor 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。

回答by Bill K

Use a hashtable to store your attributes.

使用哈希表来存储您的属性。

If you want more runtime flexibility, you might try Ruby or some other interpreted language.

如果您想要更多的运行时灵活性,您可以尝试使用 Ruby 或其他一些解释性语言。

回答by Thomas Danecker

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.

结果是您无法在运行时向类型添加任何属性。但是有各种替代技术。您可以使用简单的字典或更强大的东西,比如附加依赖属性

回答by Ralph Trickey

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

回答by Marc Gravell

What needs to see the attributes? If it is things like data-binding etc, TypeDescriptorshould work:

需要看什么属性?如果是数据绑定等,TypeDescriptor应该可以:

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

This only affects System.ComponentModelusage (not direct reflection), but that is often enough - for example, you can associate a TypeConvertervia the above.

这只会影响System.ComponentModel使用(不是直接反射),但这通常就足够了 - 例如,您可以TypeConverter通过上面的方式关联 a 。

If by "attributes" you mean "properties", then (again, as far as data-binding is concerned) TypeDescriptoralso has potential there - but it is non-trivial; you need to either implement ICustomTypeDescriptoron the object, or to write a CustomTypeDescriptorfor the type - and in either case, you need to write your own PropertyDescriptorimplementation (often talking to a per-instance dictionary etc). This will get used by anything that uses:

如果“属性”是指“属性”,那么(再次,就数据绑定而言)TypeDescriptor在那里也有潜力 - 但它并非微不足道;您需要ICustomTypeDescriptor在对象上实现,或者CustomTypeDescriptor为类型编写 a - 在任何一种情况下,您都需要编写自己的PropertyDescriptor实现(通常与每个实例的字典等交谈)。这将被任何使用:

// 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"); 

回答by Renaud Bompuis

I would go with PostSharp, a very elegant AOP framework(or Policy Injection).

我会选择PostSharp,一个非常优雅的AOP 框架(或Policy Injection)。

PostSharp allows you to inject custom attributes.
The blog entry refered to in the post has some code you can download to achieve your goal.

PostSharp允许您注入自定义属性
帖子中引用的博客条目有一些代码,您可以下载以实现您的目标。

回答by Josh

This post answered this for me:

这篇文章为我回答了这个问题:

Dynamically Creating Types at Runtime

在运行时动态创建类型