private static PropertyInfo GetBestMatchingProperty(string propertyName, Type type) { PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy); PropertyInfo propertyInfo1 = (PropertyInfo)null; int num = int.MaxValue; for (int index = 0; index < properties.Length; ++index) { PropertyInfo propertyInfo2 = properties[index]; if (propertyInfo2.Name == propertyName) { int distance = PropertyReflector.CalculateDistance(type, propertyInfo2.DeclaringType); if (distance == 0) { return(propertyInfo2); } if (distance > 0 && distance < num) { propertyInfo1 = propertyInfo2; num = distance; } } } return(propertyInfo1); }
private PropertyInfo GetPropertyInfo(Type type, string propertyName) { PropertyInfoCache propertyInfoCache = this.GetPropertyInfoCache(type); if (!propertyInfoCache.ContainsKey(propertyName)) { PropertyInfo matchingProperty = PropertyReflector.GetBestMatchingProperty(propertyName, type); if (matchingProperty == (PropertyInfo)null) { throw new ArgumentException(string.Format("Unable to find public property named {0} on type {1}", (object)propertyName, (object)type.FullName), propertyName); } propertyInfoCache.Add(propertyName, matchingProperty); } return(propertyInfoCache[propertyName]); }
public void SetValue(object target, string propertyName, object value) { if (propertyName.IndexOf(PropertyNameSeparator) > -1) { object target1 = target; string[] propertyList = propertyName.Split(PropertyNameSeparator); for (int level = 0; level < propertyList.Length - 1; ++level) { propertyName = propertyList[level]; target = this.GetValueImpl(target, propertyName); if (target == null) { string propertyNameString = PropertyReflector.GetPropertyNameString(propertyList, level); target = this.Construct(this.GetType(target1.GetType(), propertyNameString)); this.SetValue(target1, propertyNameString, target); } } propertyName = propertyList[propertyList.Length - 1]; } this.SetValueImpl(target, propertyName, value); }