Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to get sub properties using GetProperty

public class Address {     public string ZipCode {get; set;} }  public class Customer {     public Address Address {get; set;} } 

how can I access eitther "ZipCode" or "Address.ZipCode" with reflection? For example:

Typeof(Customer).GetProperty("ZipCode")? 
like image 425
Todd Smith Avatar asked Dec 14 '08 09:12

Todd Smith


2 Answers

You'd need something like:

PropertyInfo addressProperty = typeof(Customer).GetProperty("Address"); ProportyInfo zipCodeProperty = addressProperty.PropertyType.GetProperty("ZipCode");  object address = addressProperty.GetValue(customer, null); object zipCode = zipCodeProperty.GetValue(address, null); 

Basically if you want to take a string "Address.ZipCode" and navigate down it, you need to split it by "." and then call GetProperty on the appropriate type at every step to get the property itself, then PropertyInfo.GetValue to get the next value in the chain. Something like this:

public static object FollowPropertyPath(object value, string path) {     Type currentType = value.GetType();      foreach (string propertyName in path.Split('.'))     {         PropertyInfo property = currentType.GetProperty(propertyName);         value = property.GetValue(value, null);         currentType = property.PropertyType;     }     return value; } 

Call it like this:

object zipCode = FollowPropertyPath(customer, "Address.ZipCode"); 

Note that this works on the compile-time types of the properties. If you want it to cope with the execution time type (e.g. if customer.Address didn't have a ZipCode property, but the actual type returned by Address did) then change property.PropertyType to property.GetType().

Also note that this doesn't have any error handling etc :)

like image 110
Jon Skeet Avatar answered Sep 21 '22 23:09

Jon Skeet


Jon Skeet's answer is fine, I had to extend his method a bit though, in order to account for derived instances within the property path:

public static class ReflectorUtil {     public static object FollowPropertyPath(object value, string path)     {         if (value == null) throw new ArgumentNullException("value");         if (path == null) throw new ArgumentNullException("path");          Type currentType = value.GetType();          object obj = value;         foreach (string propertyName in path.Split('.'))         {             if (currentType != null)             {                 PropertyInfo property = null;                 int brackStart = propertyName.IndexOf("[");                 int brackEnd = propertyName.IndexOf("]");                  property = currentType.GetProperty(brackStart > 0 ? propertyName.Substring(0, brackStart) : propertyName);                 obj = property.GetValue(obj, null);                  if (brackStart > 0)                 {                     string index = propertyName.Substring(brackStart + 1, brackEnd - brackStart - 1);                     foreach (Type iType in obj.GetType().GetInterfaces())                     {                         if (iType.IsGenericType && iType.GetGenericTypeDefinition() == typeof(IDictionary<,>))                         {                             obj = typeof(ReflectorUtil).GetMethod("GetDictionaryElement")                                                  .MakeGenericMethod(iType.GetGenericArguments())                                                  .Invoke(null, new object[] { obj, index });                             break;                         }                         if (iType.IsGenericType && iType.GetGenericTypeDefinition() == typeof(IList<>))                         {                             obj = typeof(ReflectorUtil).GetMethod("GetListElement")                                                  .MakeGenericMethod(iType.GetGenericArguments())                                                  .Invoke(null, new object[] { obj, index });                             break;                         }                     }                 }                  currentType = obj != null ? obj.GetType() : null; //property.PropertyType;             }             else return null;         }         return obj;     }      public static TValue GetDictionaryElement<TKey, TValue>(IDictionary<TKey, TValue> dict, object index)     {         TKey key = (TKey)Convert.ChangeType(index, typeof(TKey), null);         return dict[key];     }      public static T GetListElement<T>(IList<T> list, object index)     {         return list[Convert.ToInt32(index)];     }  } 

Using property.PropertyType will get you the property type defined on the obj class, while using obj.GetType() will get you the actual type of the property's instance.

EDIT: @Oliver - you're absolutely right, thanks for noting that. I adjusted the method to allow for generic Lists and Dictionaries. While I don't like the parsing part, I used Marc Gravell's clever idea in this thread to get the indexer property's values.

like image 35
Mike Fuchs Avatar answered Sep 18 '22 23:09

Mike Fuchs