如何通过从具有类型层次结构的字符串中知道其名称来获取属性值

时间:2014-07-16 10:04:42

标签: c# reflection

我有以下课程hierarichy

  class firstlevel
  {
      public secondlevel sl { get; set; }
  }

  class secondlevel
  {
      public string Name { get; set; }
  }

创建了firstlevel的对象,Name设置为sandy。       var fl = new firstlevel {sl = new secondlevel {Name =" sandy" }};

这是一个例子,我不知道真正的名字中的名字,但我知道这个课程的层次结构。

我需要编写一个方法来获取Name

的值

通过阅读我在代码后写的很多东西,但令人惊讶的是它给了我这个属性的名称而不是它的价值,我想知道我的代码有什么问题,任何人都可以解决它。

  public static Object GetValue()
  {
      var fl = new firstlevel { sl = new secondlevel { Name = "sandy" } };
      Object obj = fl;
      const string path = "fl.sl.Name";

      String[] part = path.Split('.');
      Type type = obj.GetType();

      string firstPart = part[0];
      string secondpart = part[1];
      string thirdpart = part[2];

      PropertyInfo info = type.GetProperty(secondpart);
      if (info == null) { return null; }

      PropertyInfo info1 = info.GetType().GetProperty(thirdpart);
      if (info1 == null) { return null; }

      return info1.GetValue(info1, null);
  }

2 个答案:

答案 0 :(得分:1)

这可能有效

public static Object GetValue()

  {

        var fl = new firstlevel { sl = new secondlevel { Name = "sandy" } };
        Object obj = fl;
        const string path = "fl.sl.Name";

        String[] part = path.Split('.');
        Type type = obj.GetType();

        string firstPart = part[0];
        string secondpart = part[1];
        string thirdpart = part[2];

        System.Reflection.PropertyInfo info = type.GetProperty(secondpart);
        if (info == null) { return null; }


        System.Reflection.PropertyInfo info1 = info.PropertyType.GetProperty(thirdpart);
        if (info1 == null) { return null; }


        return info1.GetValue(fl.sl, null);
}

答案 1 :(得分:0)

他误以为我没有得到内部对象的Value(实例)来到最后一个属性,而是我直接尝试按类型访问它,这是错误的。

正确的答案如下:

   public static Object GetValue()
   {
       var fl = new firstlevel { sl = new secondlevel { Name = "imran" } };
       Object obj = fl;
       const string path = "fl.sl.Name";

       var part = path.Split('.');
       var type = obj.GetType();

       var firstPart = part[0];
       var secondpart = part[1];
       var thirdpart = part[2];

       var info = type.GetProperty(secondpart);
       if (info == null) { return null; }

       var secondObject = info.GetValue(obj, null);
       return secondObject.GetType().GetProperty(thirdpart).GetValue(secondObject);
   }

这很好用,以下是任何级别的hierarichy的通用方法

  public static Object GetValueFromClassProperty(String typeHierarichy, Object parentClassObject)
  {
      foreach (String part in typeHierarichy.Split('.'))
      {
          if (parentClassObject == null) { return null; }

          Type type = parentClassObject.GetType();
          PropertyInfo info = type.GetProperty(part);
          if (info == null) { return null; }

          parentClassObject = info.GetValue(parentClassObject, null);
      }
      return parentClassObject;
  }

通话就像

 string value = GetValueFromClassProperty(fl,"sl.Name");

我还找到了一个很棒的类Reflector,它可以完成所有这些以及更多功能并作为开源提供。