System.DefaultBinder.FindMostSpecificMethod C# (CSharp) Method

FindMostSpecificMethod() private static method

private static FindMostSpecificMethod ( MethodBase m1, int paramOrder1, Type paramArrayType1, MethodBase m2, int paramOrder2, Type paramArrayType2, Type types, Object args ) : int
m1 System.Reflection.MethodBase
paramOrder1 int
paramArrayType1 Type
m2 System.Reflection.MethodBase
paramOrder2 int
paramArrayType2 Type
types Type
args Object
return int
        private static int FindMostSpecificMethod(MethodBase m1, int[] paramOrder1, Type paramArrayType1,
                                                  MethodBase m2, int[] paramOrder2, Type paramArrayType2,
                                                  Type[] types, Object[] args)
        {
            // Find the most specific method based on the parameters.
            int res = FindMostSpecific(m1.GetParametersNoCopy(), paramOrder1, paramArrayType1,
                                       m2.GetParametersNoCopy(), paramOrder2, paramArrayType2, types, args);            

            // If the match was not ambigous then return the result.
            if (res != 0)
                return res;

            // Check to see if the methods have the exact same name and signature.
            if (CompareMethodSigAndName(m1, m2))
            {
                // Determine the depth of the declaring types for both methods.
                int hierarchyDepth1 = GetHierarchyDepth(m1.DeclaringType);
                int hierarchyDepth2 = GetHierarchyDepth(m2.DeclaringType);

                // The most derived method is the most specific one.
                if (hierarchyDepth1 == hierarchyDepth2) 
                {
                    return 0; 
                }
                else if (hierarchyDepth1 < hierarchyDepth2) 
                {
                    return 2;
                }
                else
                {
                    return 1;
                }
            }

            // The match is ambigous.
            return 0;
        }

Usage Example

        public override MethodBase SelectMethod(BindingFlags bindingAttr, MethodBase[] match, Type[] types, ParameterModifier[] modifiers)
        {
            Type[] array = new Type[types.Length];
            for (int i = 0; i < types.Length; i++)
            {
                array[i] = types[i].UnderlyingSystemType;
                if (!(array[i] is RuntimeType))
                {
                    throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "types");
                }
            }
            types = array;
            if (match == null || match.Length == 0)
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_EmptyArray"), "match");
            }
            MethodBase[] array2 = (MethodBase[])match.Clone();
            int          num    = 0;

            for (int i = 0; i < array2.Length; i++)
            {
                ParameterInfo[] parametersNoCopy = array2[i].GetParametersNoCopy();
                if (parametersNoCopy.Length == types.Length)
                {
                    int j;
                    for (j = 0; j < types.Length; j++)
                    {
                        Type parameterType = parametersNoCopy[j].ParameterType;
                        if (!(parameterType == types[j]) && !(parameterType == typeof(object)))
                        {
                            if (parameterType.IsPrimitive)
                            {
                                if (!(types[j].UnderlyingSystemType is RuntimeType))
                                {
                                    break;
                                }
                                if (!DefaultBinder.CanConvertPrimitive((RuntimeType)types[j].UnderlyingSystemType, (RuntimeType)parameterType.UnderlyingSystemType))
                                {
                                    break;
                                }
                            }
                            else if (!parameterType.IsAssignableFrom(types[j]))
                            {
                                break;
                            }
                        }
                    }
                    if (j == types.Length)
                    {
                        array2[num++] = array2[i];
                    }
                }
            }
            if (num == 0)
            {
                return(null);
            }
            if (num == 1)
            {
                return(array2[0]);
            }
            int  num2 = 0;
            bool flag = false;

            int[] array3 = new int[types.Length];
            for (int i = 0; i < types.Length; i++)
            {
                array3[i] = i;
            }
            for (int i = 1; i < num; i++)
            {
                int num3 = DefaultBinder.FindMostSpecificMethod(array2[num2], array3, null, array2[i], array3, null, types, null);
                if (num3 == 0)
                {
                    flag = true;
                }
                else if (num3 == 2)
                {
                    flag = false;
                    num2 = i;
                }
            }
            if (flag)
            {
                throw new AmbiguousMatchException(Environment.GetResourceString("Arg_AmbiguousMatchException"));
            }
            return(array2[num2]);
        }
All Usage Examples Of System.DefaultBinder::FindMostSpecificMethod