System.DefaultBinder.FindMostSpecificProperty C# (CSharp) Method

FindMostSpecificProperty() private static method

private static FindMostSpecificProperty ( PropertyInfo cur1, PropertyInfo cur2 ) : int
cur1 System.Reflection.PropertyInfo
cur2 System.Reflection.PropertyInfo
return int
        private static int FindMostSpecificProperty(PropertyInfo cur1,PropertyInfo cur2)
        {
            // Check to see if the fields have the same name.
            if (cur1.Name == cur2.Name)
            {
                int hierarchyDepth1 = GetHierarchyDepth(cur1.DeclaringType);
                int hierarchyDepth2 = GetHierarchyDepth(cur2.DeclaringType);

                if (hierarchyDepth1 == hierarchyDepth2) {
                    return 0; 
                }
                else if (hierarchyDepth1 < hierarchyDepth2) 
                    return 2;
                else
                    return 1;
            }

            // The match is ambigous.
            return 0;
        }
        

Usage Example

        public override PropertyInfo SelectProperty(BindingFlags bindingAttr, PropertyInfo[] match, Type returnType, Type[] indexes, ParameterModifier[] modifiers)
        {
            if (indexes != null)
            {
                if (!Contract.ForAll <Type>(indexes, (Type t) => t != null))
                {
                    Exception ex = new ArgumentNullException("indexes");
                    throw ex;
                }
            }
            if (match == null || match.Length == 0)
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_EmptyArray"), "match");
            }
            PropertyInfo[] array = (PropertyInfo[])match.Clone();
            int            i     = 0;
            int            num   = 0;
            int            num2  = (indexes != null) ? indexes.Length : 0;
            int            j     = 0;

            while (j < array.Length)
            {
                if (indexes == null)
                {
                    goto IL_118;
                }
                ParameterInfo[] indexParameters = array[j].GetIndexParameters();
                if (indexParameters.Length == num2)
                {
                    for (i = 0; i < num2; i++)
                    {
                        Type parameterType = indexParameters[i].ParameterType;
                        if (!(parameterType == indexes[i]) && !(parameterType == typeof(object)))
                        {
                            if (parameterType.IsPrimitive)
                            {
                                if (!(indexes[i].UnderlyingSystemType is RuntimeType))
                                {
                                    break;
                                }
                                if (!DefaultBinder.CanConvertPrimitive((RuntimeType)indexes[i].UnderlyingSystemType, (RuntimeType)parameterType.UnderlyingSystemType))
                                {
                                    break;
                                }
                            }
                            else if (!parameterType.IsAssignableFrom(indexes[i]))
                            {
                                break;
                            }
                        }
                    }
                    goto IL_118;
                }
IL_182:
                j++;
                continue;
IL_118:
                if (i == num2)
                {
                    if (returnType != null)
                    {
                        if (array[j].PropertyType.IsPrimitive)
                        {
                            if (!(returnType.UnderlyingSystemType is RuntimeType))
                            {
                                goto IL_182;
                            }
                            if (!DefaultBinder.CanConvertPrimitive((RuntimeType)returnType.UnderlyingSystemType, (RuntimeType)array[j].PropertyType.UnderlyingSystemType))
                            {
                                goto IL_182;
                            }
                        }
                        else if (!array[j].PropertyType.IsAssignableFrom(returnType))
                        {
                            goto IL_182;
                        }
                    }
                    array[num++] = array[j];
                    goto IL_182;
                }
                goto IL_182;
            }
            if (num == 0)
            {
                return(null);
            }
            if (num == 1)
            {
                return(array[0]);
            }
            int  num3 = 0;
            bool flag = false;

            int[] array2 = new int[num2];
            for (j = 0; j < num2; j++)
            {
                array2[j] = j;
            }
            for (j = 1; j < num; j++)
            {
                int num4 = DefaultBinder.FindMostSpecificType(array[num3].PropertyType, array[j].PropertyType, returnType);
                if (num4 == 0 && indexes != null)
                {
                    num4 = DefaultBinder.FindMostSpecific(array[num3].GetIndexParameters(), array2, null, array[j].GetIndexParameters(), array2, null, indexes, null);
                }
                if (num4 == 0)
                {
                    num4 = DefaultBinder.FindMostSpecificProperty(array[num3], array[j]);
                    if (num4 == 0)
                    {
                        flag = true;
                    }
                }
                if (num4 == 2)
                {
                    flag = false;
                    num3 = j;
                }
            }
            if (flag)
            {
                throw new AmbiguousMatchException(Environment.GetResourceString("Arg_AmbiguousMatchException"));
            }
            return(array[num3]);
        }
All Usage Examples Of System.DefaultBinder::FindMostSpecificProperty