1

I have this string
Member.User.Name

and this instance:

Root root = new Root();
root.Member.User.Name = "user name";

How do I extract the value from root of the member Member.User.Name

for example:

string res = GetDeepPropertyValue(root, "Member.User.Name");

res will be "user name"

Thanks

SexyMF
  • 10,657
  • 33
  • 102
  • 206
  • 3
    `.GetProperty()` recursively? Your question is unclear and does not show research effort – Sayse Sep 18 '13 at 10:49

2 Answers2

5

Try this:

public object GetDeepPropertyValue(object instance, string path){
  var pp = path.Split('.');
  Type t = instance.GetType();
  foreach(var prop in pp){
    PropertyInfo propInfo = t.GetProperty(prop);
    if(propInfo != null){
      instance = propInfo.GetValue(instance, null);
      t = propInfo.PropertyType;
    }else throw new ArgumentException("Properties path is not correct");
  }
  return instance;
}
string res = GetDeepPropertyValue(root, "Member.User.Name").ToString();

NOTE: We don't need recursive solution for this because the number of loops is known beforehand. Using foreach would be more efficient if possible. We use recursion only when the implementation becomes complicated with for - foreach.

King King
  • 61,710
  • 16
  • 105
  • 130
  • 1
    You shouldn't use `propInfo.PropertyType` but `instance.GetType()` as the actual value that the property holds may be derived from the property type. – Adi Lester Sep 18 '13 at 11:50
  • @AdiLester it may be right, but the `Properties path` is the path in which all the properties should be defined in the classes, that means using `PropertyType` will be OK. – King King Sep 18 '13 at 11:54
  • 1
    What do you mean by "should be defined"? If I have a property `object MyObj { get; set; }` and I assign it a `Person` object, I'd expect this method to give me the person's name for `MyObj.Name` and not throw an exception. At least that's how property paths work in other places like binding in WPF. – Adi Lester Sep 18 '13 at 11:57
  • @AdiLester that's not what the OP wants, I mean all the defined classes will show its properties with type declared clearly, based on that we can build the `properties path` and access at runtime. However your idea is very good for achieving the value more dynamically. While using `PropertyInfo` only return value when the `path` can be derived from the classes definitions. – King King Sep 18 '13 at 12:00
  • Unfortunately, this solution doesn't seem to work against dynamic objects... – JustMaier Aug 01 '14 at 04:22
  • It does work however if you make the change @AdiLester mentioned... – JustMaier Aug 01 '14 at 04:33
4

Shazam I believe you are looking to access the property using reflection in recursive way

public static object GetDeepPropertyValue(object src, string propName)
    {
        if (propName.Contains('.'))
        {
            string[] Split = propName.Split('.');
            string RemainingProperty = propName.Substring(propName.IndexOf('.') + 1);
            return GetDeepPropertyValue(src.GetType().GetProperty(Split[0]).GetValue(src, null), RemainingProperty);
        }
        else
            return src.GetType().GetProperty(propName).GetValue(src, null);
    }

Don't forget to add validation checks if needed.

Deepak Bhatia
  • 6,230
  • 2
  • 24
  • 58