【问题标题】:Setting/getting the class properties by string name [duplicate]通过字符串名称设置/获取类属性[重复]
【发布时间】:2012-05-04 05:19:18
【问题描述】:

我要做的是使用字符串设置类中的属性值。例如,我的类具有以下属性:

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

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

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

理想情况下,我只想分配一个变量,然后使用变量中的字符串名称设置属性:

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

我正在阅读有关反射的内容,也许这是这样做的方法,但我不确定如何进行设置,同时保持类中的属性访问不变。我希望仍然能够使用:

myClass.Name = "John"

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

【问题讨论】:

  • 我正在尝试这样做,因为我正在从数据库中获取数据转储,并且我只是有选择地想要挑选出我需要存储在我的班级中的字段。基本上我不想检查每个项目并存储在课堂上。我需要遍历所有字段,只需要动态地挑选项目并将其添加到类中。

标签: c# reflection properties


【解决方案1】:

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

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

【讨论】:

  • 你,我的朋友,绝对是个天才!谢谢!
  • 不错!我建议将 typeof(MyClass) 替换为 GetType() 以使其更通用,例如在抽象类中使用它。 :)
  • 不错的解决方案!但不适用于字典类型的属性,Visual Studio 在尝试添加值时会出现错误 - 没有添加的定义。思想仅限于设置值,因此有一种解决方法是创建一个临时字典“D”并将 de 值设置为“D”。
【解决方案2】:

您可以在类中添加索引器并使用反射来获得属性:

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

【讨论】:

    【解决方案3】:

    可能是这样的?

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

    【讨论】:

      猜你喜欢
      • 2017-01-03
      • 1970-01-01
      • 2019-03-12
      • 1970-01-01
      • 2012-07-08
      • 2013-02-11
      • 1970-01-01
      相关资源
      最近更新 更多