Пример #1
0
        internal static object BuildType(Type type, ObtainParameterCallback obtainParameter)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (obtainParameter == null)
            {
                throw new ArgumentNullException(nameof(obtainParameter));
            }

            if (type.IsInterface || type.IsAbstract)
            {
                throw new ArgumentException($"Unable to create an instance of the specified '{type.FullName}' type because it is an interface or an abstract class.");
            }

            ConstructorInfo[] constructors = type.GetConstructors();

            foreach (ConstructorInfo constructor in constructors.OrderByDescending(ctor => ctor.GetParameters().Length))
            {
                ParameterInfo[] parameters = constructor.GetParameters();

                if (parameters.Length == 0)
                {
                    return(Activator.CreateInstance(type));
                }

                bool     matched = false;
                object[] values  = new object[parameters.Length];

                for (int i = 0; i < parameters.Length; i++)
                {
                    //依次获取当前构造函数的参数值
                    matched = obtainParameter(parameters[i].ParameterType, parameters[i].Name, out values[i]);

                    //如果获取参数值失败,则当前构造函数匹配失败
                    if (!matched)
                    {
                        break;
                    }
                }

                if (matched)
                {
                    return(Activator.CreateInstance(type, values));
                }
            }

            return(null);
        }
Пример #2
0
        internal static object BuildType(Type type, ObtainParameterCallback obtainParameter)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            if (obtainParameter == null)
            {
                throw new ArgumentNullException("obtainParameter");
            }

            ConstructorInfo[] constructors = type.GetConstructors();

            foreach (ConstructorInfo constructor in constructors.OrderByDescending(ctor => ctor.GetParameters().Length))
            {
                ParameterInfo[] parameters = constructor.GetParameters();

                if (parameters.Length == 0)
                {
                    return(Activator.CreateInstance(type));
                }

                bool     matched = false;
                object[] values  = new object[parameters.Length];

                for (int i = 0; i < parameters.Length; i++)
                {
                    //依次获取当前构造函数的参数值
                    matched = obtainParameter(parameters[i].ParameterType, parameters[i].Name, out values[i]);

                    //如果获取参数值失败,则当前构造函数匹配失败
                    if (!matched)
                    {
                        break;
                    }
                }

                if (matched)
                {
                    return(Activator.CreateInstance(type, values));
                }
            }

            return(null);
        }