Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Address
- {
- public string ZipCode {get; set;}
- }
- public class Customer
- {
- public Address Address {get; set;}
- }
- Typeof(Customer).GetProperty("ZipCode")?
- PropertyInfo addressProperty = typeof(Customer).GetProperty("Address");
- ProportyInfo zipCodeProperty = addressProperty.PropertyType.GetProperty("ZipCode");
- object address = addressProperty.GetProperty(customer, null);
- object zipCode = zipCodeProperty.GetProperty(address, null);
- 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;
- }
- object zipCode = FollowPropertyPath(customer, "Address.ZipCode");
- static void Main()
- {
- object obj = new Customer { Address = new Address { ZipCode = "abcdef" } };
- object address = GetValue(obj, "Address");
- object zip = GetValue(address, "ZipCode");
- Console.WriteLine(zip);
- }
- static object GetValue(object component, string propertyName)
- {
- return TypeDescriptor.GetProperties(component)[propertyName].GetValue(component);
- }
- static object ResolveValue(object component, string path) {
- foreach(string segment in path.Split('.')) {
- if (component == null) return null;
- if(component is IListSource) {
- component = ((IListSource)component).GetList();
- }
- if (component is IList) {
- component = ((IList)component)[0];
- }
- component = GetValue(component, segment);
- }
- return component;
- }
- 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)];
- }
- }
- typeof (Customer).GetProperty("Address").PropertyType.GetProperty("ZipCode")
Add Comment
Please, Sign In to add comment