C#设置/通过字符串名称获取类属性

问题描述:

我想要做的是使用字符串设置类的属性的值。例如,我的类具有以下属性:C#设置/通过字符串名称获取类属性

myClass.Name 
myClass.Address 
myClass.PhoneNumber 
myClass.FaxNumber 

,所有字段都是字符串类型,所以我提前知道的时候,它总是一个字符串。现在我希望能够像使用DataSet对象一样使用字符串来设置属性。事情是这样的:

myClass["Name"] = "John" 
myClass["Address"] = "1112 River St., Boulder, CO" 

理想我想只分配一个变量,然后使用该字符串名称从变量

string propName = "Name" 
myClass[propName] = "John" 

我读到关于反射设置属性,也许这是应该做的方式但我不确定如何设定这一点,同时保持课堂上完好无损的财产。我想仍然可以使用

myClass.Name = "John" 

任何代码示例都会非常棒。

+0

可能的重复http://*.com/questions/1089123/setting-a-property-by-reflection-with-a-string-value – Thomas 2012-04-23 15:12:49

+0

看看这个:http:// *。 com/questions/279374 /我如何使用net-reflection-to-search-for-a-property-by-name-ignore-case – MarcinJuraszek 2012-04-23 15:13:29

+0

请记住,反射速度很慢。如果表现很重要,那么不推荐。 – 2012-04-23 15:17:19

您可以添加索引器属性,一个

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

     } 

    } 
} 
+1

我觉得你应该在'GetProperty(“propertyName”)''中移除'''',并且'set'部分应该使用'SetValue'部分 – Marco 2012-04-23 15:16:41

+0

@Marco:刚刚完成:) – Tigran 2012-04-23 15:17:15

+0

非常优雅,谢谢 – Patratacus 2012-04-23 16:04:46

可能是这样呢?

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>(); 
    } 
} 
+0

您还必须在构造函数中填充字典以使其工作。 – 2012-04-23 15:20:37

+0

是的,这只是一个进一步实施的提议 – Deitro 2012-04-23 15:25:15

+0

那么如果实际属性值是不同类型的呢? – 2013-10-18 11:13:50

您可以将索引添加到您的类并使用反射来尖子属性:

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; 
     } 
    } 
} 

的任何Class

public class Foo 
{ 
    public object this[string propertyName] 
    { 
     get { return this.GetType().GetProperty(propertyName).GetValue(this, null); } 
     set { this.GetType().GetProperty(propertyName).SetValue(this, value, null); } 
    } 

    public string Bar { get; set; } 
} 

然后,你可以使用:

Foo f = new Foo(); 
// Set 
f["Bar"] = "asdf"; 
// Get 
string s = (string)f["Bar"]; 

酸ce:Get property value from string using reflection in C#

+0

与字典解决方案相比,性能如何? – 2014-09-27 14:19:23

+0

非常慢!但问题是“C#设置/获取字符串名称的类属性”,关于“类”,而不是“字典” – 2014-09-28 18:16:54

+1

@ cristian-e哪一个是字典解决方案? – dsi 2017-03-17 14:02:00