向类动态添加属性 C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16585679/
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
Add properties to class dynamically C#
提问by Pavan Dhariwal
I want to add properties to a class dynamically and set values to them. Could someone point me in the right direction to some tutorials or articles or even better provide an example.
我想动态地向类添加属性并为它们设置值。有人可以为我指出一些教程或文章的正确方向,或者更好地提供一个示例。
I have a base class which is called in a foreach loop. Inside this class I need to add up to 30 properties and set values to them.
我有一个在 foreach 循环中调用的基类。在这个类中,我需要添加多达 30 个属性并为它们设置值。
This is the method that calls the class.
这是调用类的方法。
foreach (Class name in this.list)
{
ClassBeingCalled class = new ClassBeingCalled (name);
class.Populate();
this.newlist.Add(class);
}
Now inside the class being called, I need to create the 30 or so properties on the fly, which will be set in the "Populate" method of that class.
现在在被调用的类中,我需要动态创建 30 个左右的属性,这些属性将在该类的“Populate”方法中设置。
Like so...
像这样...
foreach (PropertyToAssign count2 in listofproperties)
{
string name = "_nameofproperty" + count2.name
base.GetType().GetField(name, BindingFlags.NonPublic |
BindingFlags.Instance).SetValue(this, count2);
}
回答by Rudi Visser
There are a few ways to approach this.
有几种方法可以解决这个问题。
a) You use reflection to generate a class at runtime with whatever you need. You can always derive this generated class from a base class that contains Populatebut I don't see the need.
a) 您可以使用反射在运行时根据需要生成一个类。您始终可以从包含Populate但我认为不需要的基类派生这个生成的类。
b) If you're using C# 4.0+, you could use ExpandoObject, which allows you to set whatever properties you want for your needs. Note that these are notadded as properties per se, as it uses the DLR which is all runtime-based. Depending on your needs, this may be fine; this is pretty much the same as approach c, but using the DLR.
b) 如果您使用的是 C# 4.0+,则可以使用ExpandoObject,它允许您根据需要设置所需的任何属性。请注意,这些本身并不是作为属性添加的,因为它使用的 DLR 都是基于运行时的。根据您的需要,这可能没问题;这与方法 c 几乎相同,但使用的是 DLR。
c) You could use a backing dictionary and use an indexer, like so:
c) 您可以使用支持字典并使用索引器,如下所示:
private Dictionary<string, object> _internalData = new Dictionary<string, object>();
public T this[string propName]
{
get {
return _internalData[propName];
}
set {
_internalData[propName] = value;
}
}
By using approach 3, you're actually indexing based on strings which is probably better based on your requirements... As you can do something along the lines of this:
通过使用方法 3,您实际上是基于strings 进行索引,这可能更好地基于您的要求......因为您可以按照以下方式做一些事情:
foreach (PropertyToAssign count2 in listofproperties)
{
string name = "_nameofproperty" + count2.name;
this[name] = count2.value;
}
At the end of the day, it's probably best if you explain the problem you're trying to solve, as there may be a better way to approach it entirely.
归根结底,最好解释一下要解决的问题,因为可能有更好的方法来完全解决它。
回答by Mehmet Ata?
It is not possible to add dynamic properties to a compiled class on the fly. You can only add dynamic properties to classes while building the class itself on the fly using Reflection.Emit
无法动态地将动态属性添加到已编译的类中。您只能在使用Reflection.Emit即时构建类本身时向类添加动态属性
Your other option could be using a Dictionary or ExpandoObject(which is actually a dictionary). But for this option, you cannot access your dynamic properties using reflection.
您的另一个选择可能是使用 Dictionary 或ExpandoObject(实际上是一个字典)。但是对于此选项,您无法使用反射访问动态属性。

