public class Address
{
public string ZipCode {get; set;}
}
public class Customer
{
public Address Address {get; set;}
}
how can I access e
Problem: Weakly Typed Variables:
@jonskeet's FollowPropertyPath(...) method almost met my needs exactly; except that
my property was weakly typed; therefore, currentType = property.PropertyType simply returned System.Object and failed on the next iteration of the foreach-loop.
Solution: To use the runtime type rather than the design-time type, I adjusted the method as follows:
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 = value.GetType(); // <-- Change
}
return value;
}