示例#1
0
        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);
        }