C# 通过字符串名称设置/获取类属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10283206/
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
Setting/getting the class properties by string name
提问by Patratacus
What I'm trying to do is setting the value of the property in a class using a string. For example, my class has the following properties:
我想要做的是使用字符串在类中设置属性的值。例如,我的类具有以下属性:
myClass.Name
myClass.Address
myClass.PhoneNumber
myClass.FaxNumber
All the fields are of stringtype so I know ahead of time that it's always a string. Now, I want to be able to set the properties using a string as you could do with a DataSetobject. Something like this:
所有字段都是string类型,所以我提前知道它总是一个字符串。现在,我希望能够像处理DataSet对象一样使用字符串设置属性。像这样的东西:
myClass["Name"] = "John"
myClass["Address"] = "1112 River St., Boulder, CO"
Ideally, I want to just assign a variable and then set the property using that string name from the variable:
理想情况下,我只想分配一个变量,然后使用变量中的字符串名称设置属性:
string propName = "Name"
myClass[propName] = "John"
I was reading about reflection and maybe it's the way to do it but I'm not sure how to go about setting that up while keeping the property access intact in the class. I want to still be able to use:
我正在阅读关于反射的信息,也许这是实现它的方法,但我不确定如何在保持类中的属性访问完整的同时进行设置。我希望仍然能够使用:
myClass.Name = "John"
Any code examples would be really great.
任何代码示例都非常棒。
采纳答案by Tigran
You can add indexer property, a pseudocode:
您可以添加索引器属性,伪代码:
public class MyClass
{
public object this[string propertyName]
{
get{
// probably faster without reflection:
// like: return Properties.Settings.Default.PropertyValues[propertyName]
// instead of the following
Type myType = typeof(MyClass);
PropertyInfo myPropInfo = myType.GetProperty(propertyName);
return myPropInfo.GetValue(this, null);
}
set{
Type myType = typeof(MyClass);
PropertyInfo myPropInfo = myType.GetProperty(propertyName);
myPropInfo.SetValue(this, value, null);
}
}
}
回答by Deitro
May be something like this?
可能是这样的吗?
public class PropertyExample
{
private readonly Dictionary<string, string> _properties;
public string FirstName
{
get { return _properties["FirstName"]; }
set { _properties["FirstName"] = value; }
}
public string LastName
{
get { return _properties["LastName"]; }
set { _properties["LastName"] = value; }
}
public string this[string propertyName]
{
get { return _properties[propertyName]; }
set { _properties[propertyName] = value; }
}
public PropertyExample()
{
_properties = new Dictionary<string, string>();
}
}
回答by Dennis Traub
You can add an indexer to your class and use reflection to aces the properties:
您可以向类中添加索引器并使用反射来获取属性:
using System.Reflection;
public class MyClass {
public object this[string name]
{
get
{
var properties = typeof(MyClass)
.GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var property in properties)
{
if (property.Name == name && property.CanRead)
return property.GetValue(this, null);
}
throw new ArgumentException("Can't find property");
}
set {
return;
}
}
}

