CanConvertPrimitive() private method

private CanConvertPrimitive ( RuntimeType source, RuntimeType target ) : bool
source RuntimeType
target RuntimeType
return bool
Beispiel #1
0
        private static int FindMostSpecificType(Type c1, Type c2, Type t)
        {
            if (c1 == c2)
            {
                return(0);
            }
            if (c1 == t)
            {
                return(1);
            }
            if (c2 == t)
            {
                return(2);
            }
            if (c1.IsByRef || c2.IsByRef)
            {
                if (c1.IsByRef && c2.IsByRef)
                {
                    c1 = c1.GetElementType();
                    c2 = c2.GetElementType();
                }
                else if (c1.IsByRef)
                {
                    if (c1.GetElementType() == c2)
                    {
                        return(2);
                    }
                    c1 = c1.GetElementType();
                }
                else
                {
                    if (c2.GetElementType() == c1)
                    {
                        return(1);
                    }
                    c2 = c2.GetElementType();
                }
            }
            bool flag1;
            bool flag2;

            if (c1.IsPrimitive && c2.IsPrimitive)
            {
                flag1 = DefaultBinder.CanConvertPrimitive((RuntimeType)c2, (RuntimeType)c1);
                flag2 = DefaultBinder.CanConvertPrimitive((RuntimeType)c1, (RuntimeType)c2);
            }
            else
            {
                flag1 = c1.IsAssignableFrom(c2);
                flag2 = c2.IsAssignableFrom(c1);
            }
            if (flag1 == flag2)
            {
                return(0);
            }
            return(flag1 ? 2 : 1);
        }
        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]);
        }
        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]);
        }
Beispiel #4
0
        public override PropertyInfo SelectProperty(BindingFlags bindingAttr, PropertyInfo[] match, Type returnType, Type[] indexes, ParameterModifier[] modifiers)
        {
            if (indexes != null)
            {
                Type[]           typeArray  = indexes;
                Predicate <Type> predicate1 = (Predicate <Type>)(t => t != (Type)null);
                Predicate <Type> predicate2;
                if (!Contract.ForAll <Type>((IEnumerable <Type>)typeArray, predicate2))
                {
                    throw new ArgumentNullException("indexes");
                }
            }
            if (match == null || match.Length == 0)
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_EmptyArray"), "match");
            }
            PropertyInfo[] propertyInfoArray = (PropertyInfo[])match.Clone();
            int            index1            = 0;
            int            num1   = 0;
            int            length = indexes != null ? indexes.Length : 0;

            for (int index2 = 0; index2 < propertyInfoArray.Length; ++index2)
            {
                if (indexes != null)
                {
                    ParameterInfo[] indexParameters = propertyInfoArray[index2].GetIndexParameters();
                    if (indexParameters.Length == length)
                    {
                        for (index1 = 0; index1 < length; ++index1)
                        {
                            Type parameterType = indexParameters[index1].ParameterType;
                            if (!(parameterType == indexes[index1]) && !(parameterType == typeof(object)))
                            {
                                if (parameterType.IsPrimitive)
                                {
                                    if (!(indexes[index1].UnderlyingSystemType is RuntimeType) || !DefaultBinder.CanConvertPrimitive((RuntimeType)indexes[index1].UnderlyingSystemType, (RuntimeType)parameterType.UnderlyingSystemType))
                                    {
                                        break;
                                    }
                                }
                                else if (!parameterType.IsAssignableFrom(indexes[index1]))
                                {
                                    break;
                                }
                            }
                        }
                    }
                    else
                    {
                        continue;
                    }
                }
                if (index1 == length)
                {
                    if (returnType != (Type)null)
                    {
                        if (propertyInfoArray[index2].PropertyType.IsPrimitive)
                        {
                            if (!(returnType.UnderlyingSystemType is RuntimeType) || !DefaultBinder.CanConvertPrimitive((RuntimeType)returnType.UnderlyingSystemType, (RuntimeType)propertyInfoArray[index2].PropertyType.UnderlyingSystemType))
                            {
                                continue;
                            }
                        }
                        else if (!propertyInfoArray[index2].PropertyType.IsAssignableFrom(returnType))
                        {
                            continue;
                        }
                    }
                    propertyInfoArray[num1++] = propertyInfoArray[index2];
                }
            }
            if (num1 == 0)
            {
                return((PropertyInfo)null);
            }
            if (num1 == 1)
            {
                return(propertyInfoArray[0]);
            }
            int  index3 = 0;
            bool flag   = false;

            int[] numArray = new int[length];
            for (int index2 = 0; index2 < length; ++index2)
            {
                numArray[index2] = index2;
            }
            for (int index2 = 1; index2 < num1; ++index2)
            {
                int num2 = DefaultBinder.FindMostSpecificType(propertyInfoArray[index3].PropertyType, propertyInfoArray[index2].PropertyType, returnType);
                if (num2 == 0 && indexes != null)
                {
                    num2 = DefaultBinder.FindMostSpecific(propertyInfoArray[index3].GetIndexParameters(), numArray, (Type)null, propertyInfoArray[index2].GetIndexParameters(), numArray, (Type)null, indexes, (object[])null);
                }
                if (num2 == 0)
                {
                    num2 = DefaultBinder.FindMostSpecificProperty(propertyInfoArray[index3], propertyInfoArray[index2]);
                    if (num2 == 0)
                    {
                        flag = true;
                    }
                }
                if (num2 == 2)
                {
                    flag   = false;
                    index3 = index2;
                }
            }
            if (flag)
            {
                throw new AmbiguousMatchException(Environment.GetResourceString("Arg_AmbiguousMatchException"));
            }
            return(propertyInfoArray[index3]);
        }
Beispiel #5
0
        public override MethodBase SelectMethod(BindingFlags bindingAttr, MethodBase[] match, Type[] types, ParameterModifier[] modifiers)
        {
            Type[] typeArray = new Type[types.Length];
            for (int index = 0; index < types.Length; ++index)
            {
                typeArray[index] = types[index].UnderlyingSystemType;
                if (!(typeArray[index] is RuntimeType))
                {
                    throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "types");
                }
            }
            types = typeArray;
            if (match == null || match.Length == 0)
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_EmptyArray"), "match");
            }
            MethodBase[] methodBaseArray = (MethodBase[])match.Clone();
            int          num             = 0;

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

            int[] numArray = new int[types.Length];
            for (int index1 = 0; index1 < types.Length; ++index1)
            {
                numArray[index1] = index1;
            }
            for (int index1 = 1; index1 < num; ++index1)
            {
                switch (DefaultBinder.FindMostSpecificMethod(methodBaseArray[index3], numArray, (Type)null, methodBaseArray[index1], numArray, (Type)null, types, (object[])null))
                {
                case 0:
                    flag = true;
                    break;

                case 2:
                    flag   = false;
                    index3 = index1;
                    break;
                }
            }
            if (flag)
            {
                throw new AmbiguousMatchException(Environment.GetResourceString("Arg_AmbiguousMatchException"));
            }
            return(methodBaseArray[index3]);
        }