【问题标题】:Can I use reflection and a string to get/set the correct property of an object?我可以使用反射和字符串来获取/设置对象的正确属性吗?
【发布时间】:2009-12-30 00:44:57
【问题描述】:

我正在使用 c# 和 .NET 3.5。供应商为我们提供了一个具有 UserVarNbr1、UserVarData1、UserVarNbr2、UserVarData2、... UserVarNbrN、UserVarDataN 属性的对象。不是我编码它的方式,但尽管如此,这是我们必须使用的。应用程序中的另一个对象返回用于表示这些 UserVariables 的项目集合。

集合项具有这样的属性

public string VariableName
{
  get { return _VariableName; }
  set { _VariableName = value; }
}


public string VariableData
{
  get { return _VariableData; }
  set { _VariableData = value; }
}

我需要遍历集合并创建供应商对象的实例并设置正确的属性。 UserVarNbrN、UserVarDataN 需要放在正确的位置。注意集合返回一个VariableName为字符串“03”,这需要驱动VendorObject属性UserVarNbr3,UserVarData3**注意实际属性名中没有“0”。如何引用正确的属性来获取/设置?

var o = new VendorObj();

到目前为止,我有类似的东西。

foreach (var item in userVars)
        {
            const string propPrefix = "UserVar";
            int varNum;
            var isNum = int.TryParse(item.VariableName, out varNum);

            if(isNum)
            {
                PropertyInfo pi;
                //this is where I am stuck
                // I need to set the corresponding properties on o
                // example if varNum == 38, how do I reference
                // o.(propPrefix+"Nbr"+varNum.ToString())
                // and
                // o.(propPrefix+"Data"+varNum.ToString())
                // so I may set them? 
            }
        }

感谢任何帮助。在反思方面,我是个菜鸟。

谢谢, ~ck 在圣地亚哥

【问题讨论】:

    标签: c# reflection oop properties


    【解决方案1】:
    VendorObj vndr = new VendorObj();
    
    Console.WriteLine("\nInitial value of instance property: {0}", vndr.InstanceProperty);
    
    PropertyInfo piInstance = typeof(VendorObj).GetProperty("InstanceProperty");
    
    Object obj = piInstance.GetValue(vndr, null);
    
    piInstance.SetValue(vndr, 37, null);
    
    Console.WriteLine("Final value of instance property: {0}", vndr.InstanceProperty);
    

    【讨论】:

      【解决方案2】:

      试试这个:

      const string propPrefix = "UserVar";
      
      VendorObj o = new VendorObj();
      foreach (var item in userVars)
      {
          int varNum = 0;
          if (Int32.TryParse(item.VariableName, out varNum))
          {
              string name = String.Format("{0}Nbr{1}", propPrefix, varNum);
              o.GetType().GetProperty(name).SetValue(o, "some value", null);
          }
      }
      

      【讨论】:

      • 所有很棒的答案。谢谢大家。
      【解决方案3】:

      由于您将在一个对象上设置许多属性,因此最好获取 PropertyDescriptorCollection

      var o = new VendorObj();
      PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(o);
      foreach (var item in userVars)
      {
          const string propPrefix = "userVar";
          int varNum;
          if (int.TryParse(item.VariableName, out varNum))
          {
              PropertyDescriptor property = properties.Find(propPrefix + "Nbr" + varNum, true);
              property.SetValue(o, item.VariableData);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-27
        • 2010-10-11
        • 2011-03-05
        • 1970-01-01
        • 2011-04-12
        • 1970-01-01
        • 2016-06-16
        相关资源
        最近更新 更多