Ejemplo n.º 1
0
        private ConstructorInfo FindConstructorInfo(Type type)
        {
            List <ConstructorInfo> ctors = type.GetConstructors(Flags).ToList();

            switch (ctors.Count)
            {
            case 0:
                throw new ConstructorNotFoundException("There are no public constructors.");

            case 1:
                return(ctors[0]);    // Assume we use the only ctor
            }

            if (ParameterTypes == null)
            {
                throw new ConstructorNotFoundException("Multiple constructors defined and no search criteria given.");
            }

            ctors = ctors.Where(c => MatchUtilities.MatchesParameterTypes(c, ParameterTypes)).ToList();

            switch (ctors.Count)
            {
            case 0:
                throw new ConstructorNotFoundException("No constructors match the search criteria.");

            case 1:
                return(ctors[0]);

            default:
                var ctor = ctors.Find(c => c.GetParameters().Length == 0);
                return(ctor ?? throw new ConstructorNotFoundException("Multiple constructors match the search criteria."));
            }
        }
Ejemplo n.º 2
0
        private MethodInfo FindMethodInfo()
        {
            IEnumerable <MethodInfo> methods = ClassType.GetMethods(GetBindingFlags())
                                               .Where(m => m.Name == MemberName);

            if (GenericArguments != null)
            {
                methods = methods.Where(c => MatchUtilities.MatchesGenericArgumentCount(c, GenericArguments));
            }

            if (ParameterTypes != null)
            {
                methods = methods.Where(c => MatchUtilities.MatchesParameterCount(c, ParameterTypes));
            }

            var methodsList = methods.ToList();

            if (methodsList.Count == 0)
            {
                throw new MethodNotFoundException(MemberName);
            }

            MethodInfo method = methodsList.Single();

            if (method.IsGenericMethodDefinition)
            {
                if (GenericArguments == null)
                {
                    GenericArguments = InferGenericArguments(method);
                }

                method = method.MakeGenericMethod(GenericArguments);
            }

            return(method);
        }