private static PropertyInfo GetProperty(this Type type, string name, System.Reflection.BindingFlags bindingAttr, bool baseClass) { if (name == null) { throw new ArgumentNullException("name can not be null"); } PropertyInfo propertyInfo = null; foreach (PropertyInfo current in type.GetRuntimeProperties()) { if ((bindingAttr & System.Reflection.BindingFlags.IgnoreCase) != System.Reflection.BindingFlags.Default) { if (current.Name.ToLower() != name.ToLower()) { continue; } } else if (current.Name != name) { continue; } MethodInfo methodInfo = current.GetMethod; if (methodInfo == null) { methodInfo = current.SetMethod; } if (TypeExtensions.MatchBindingFlags(methodInfo.IsPublic, methodInfo.IsPrivate, methodInfo.IsStatic, bindingAttr, baseClass)) { if (propertyInfo != null) { throw new AmbiguousMatchException(string.Format("More than one property {0} match the query", new object[] { name })); } propertyInfo = current; } } if ((bindingAttr & System.Reflection.BindingFlags.DeclaredOnly) != System.Reflection.BindingFlags.Default) { return propertyInfo; } Type baseType = type.GetTypeInfo().BaseType; if (baseType != null && baseType != type) { PropertyInfo property = baseType.GetProperty(name, bindingAttr, true); if (propertyInfo == null) { propertyInfo = property; } else if (property != null && propertyInfo.DeclaringType != property.DeclaringType) { throw new AmbiguousMatchException(string.Format("More than one property {0} match the query", new object[] { name })); } return propertyInfo; } return propertyInfo; }
private static FieldInfo GetField(this Type type, string name, System.Reflection.BindingFlags bindingAttr, bool baseClass) { if (name == null) { throw new ArgumentNullException("name can not be null"); } FieldInfo fieldInfo = null; foreach (FieldInfo current in type.GetRuntimeFields()) { if ((bindingAttr & System.Reflection.BindingFlags.IgnoreCase) != System.Reflection.BindingFlags.Default) { if (current.Name.ToLower() != name.ToLower()) { continue; } } else if (current.Name != name) { continue; } if (TypeExtensions.MatchBindingFlags(current.IsPublic, current.IsPrivate, current.IsStatic, bindingAttr, baseClass)) { if (fieldInfo != null) { throw new AmbiguousMatchException(string.Format("More than one field {0} match the query", new object[] { name })); } fieldInfo = current; } } if ((bindingAttr & System.Reflection.BindingFlags.DeclaredOnly) != System.Reflection.BindingFlags.Default) { return(fieldInfo); } Type baseType = type.GetTypeInfo().BaseType; if (baseType != null && baseType != type) { FieldInfo field = baseType.GetField(name, bindingAttr, true); if (fieldInfo == null) { fieldInfo = field; } else if (field != null && fieldInfo.DeclaringType != field.DeclaringType) { throw new AmbiguousMatchException(string.Format("More than one field {0} match the query", new object[] { name })); } return(fieldInfo); } return(fieldInfo); }