-1

I have following class hierarichy

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

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

There is an object created of firstlevel and the Name is set to sandy. var fl = new firstlevel { sl = new secondlevel { Name = "sandy" } };

This is and example , I will not know the Name in real senario , but I know this class hierarichy.

I need to write a method to get value of Name

By reading lot of stuff I've written following code, but surprisingly it gives me name of the property not its value, I am wondering what is wrong with my code, can anybody figure it out.

  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);
  }
Imran Rizvi
  • 7,331
  • 11
  • 57
  • 101
  • possible duplicate of [Get property value from string using reflection in C#](http://stackoverflow.com/questions/1196991/get-property-value-from-string-using-reflection-in-c-sharp) – Ian Ringrose Jul 16 '14 at 10:09
  • @IanRingrose link didn't work for me, if you observer i am implementing the same way but it does not work – Imran Rizvi Jul 16 '14 at 10:24

2 Answers2

1

This might work

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);
}
nraina
  • 324
  • 2
  • 20
  • it works, thanks , But I can't use fl.sl to GetValue in code as I'll not have these objects , what I'll have is only top level object and hierarichy till property in string. – Imran Rizvi Jul 16 '14 at 12:18
0

he mistake I was making was not getting Value(instance) of the inner object to get to the last property, instead I was directly trying to access it by its type , that was wrong.

Correct ans is below:

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

This works fine , following is the generic method for any level of 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;
  }

Call is like

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

I also found a great class Reflector which does all these and many more and available as open source.

Imran Rizvi
  • 7,331
  • 11
  • 57
  • 101