Пример #1
0
 private void GetGenericType(Type param, Type type)
 {
     if (param.IsGenericParameter)
     {
         if (genericType == null)
         {
             genericType = type;
         }
         if (genericType != type)
         {
             if (TypeCheck.IsImplicitCast(genericType, type) != 0)
             {
                 genericType = type;
             }
         }
     }
     else if (param.IsArray && param.GetElementType().IsGenericParameter)
     {
         if (genericType == null)
         {
             genericType = type.GetElementType();
         }
         if (genericType != type.GetElementType())
         {
             if (TypeCheck.IsImplicitCast(genericType, type.GetElementType()) != 0)
             {
                 genericType = type.GetElementType();
             }
         }
     }
     else if (param.IsGenericType)
     {
         var    paramNestList = param.GetGenericArguments();
         Type[] typeNestList;
         if (type.IsGenericType && param.GetGenericTypeDefinition() == type.GetGenericTypeDefinition())
         {
             typeNestList = type.GetGenericArguments();
         }
         else
         {
             typeNestList =
                 TypeCheck.GetGenericUpCastInfo(type, param).Value.upCastedType.GetGenericArguments();
         }
         for (int i = 0; i < typeNestList.Length; i++)
         {
             GetGenericType(paramNestList[i], typeNestList[i]);
         }
     }
 }
Пример #2
0
        static public MethodInfo Select(Type classType, string funcName, BindingFlags bindingFlags, Type[] types)
        {
            var typesLength = types.Length;

            if (classType == null)
            {
                return(null);
            }
            var methodInfos =
                classType.GetMethods(bindingFlags)
                .Where(f => {
                if (f.Name == funcName && f.GetParameters().Length == typesLength)
                {
                    return(true);
                }
                return(false);
            });

            var tuples = methodInfos.Select(
                m => {
                var parameters = m.GetParameters();
                Tuple tuple;
                tuple.methodInfo      = m;
                tuple.paramsPrioritys =
                    parameters.
                    Zip(types, (param, type) =>
                        TypeCheck.GetParamsPriority(type, param.ParameterType));

                return(tuple);
            }).Where(tuple => !tuple.paramsPrioritys.Contains(null));

            var tempTuple = new Tuple {
                paramsPrioritys = null
            };
            int tempSum = 0;

            foreach (var it in tuples)
            {
                if (tempTuple.paramsPrioritys == null)
                {
                    tempTuple = it;
                    continue;
                }

                int sum = 0;
                sum = it.paramsPrioritys.Zip(tempTuple.paramsPrioritys,
                                             (paramsPriority, tParamsPriority) => paramsPriority.CompareTo(tParamsPriority))
                      .Sum();
                if (sum > it.methodInfo.GetParameters().Length / 2)
                {
                    tempTuple = it;
                }
            }
            if (tempTuple.methodInfo == null)
            {
                return(null);
            }
            if (tempTuple.methodInfo.IsGenericMethod)
            {
                Type genericType = new SelectGenericParams().Select(tempTuple.methodInfo.GetParameters(), types);
                if (genericType != null)
                {
                    return(tempTuple.methodInfo.MakeGenericMethod(genericType));
                }
                return(null);
            }
            return(tempTuple.methodInfo);
        }