Esempio n. 1
0
        /// <summary>
        /// Instantiate a type with constructor arguments provided directly and/or from an <see cref="IServiceProvider"/>.
        /// </summary>
        /// <param name="provider">The service provider used to resolve dependencies</param>
        /// <param name="instanceType">The type to activate</param>
        /// <param name="parameters">Constructor arguments not provided by the <paramref name="provider"/>.</param>
        /// <returns>An activated object of type instanceType</returns>
        public static object CreateInstance(IServiceProvider provider, Type instanceType, params object[] parameters)
        {
            int bestLength = -1;
            ConstructorMatcher bestMatcher = null;

            if (!instanceType.GetTypeInfo().IsAbstract)
            {
                foreach (var matcher in instanceType
                         .GetTypeInfo()
                         .DeclaredConstructors
                         .Where(c => !c.IsStatic && c.IsPublic)
                         .Select(constructor => new ConstructorMatcher(constructor)))
                {
                    var length = matcher.Match(parameters);
                    if (length == -1)
                    {
                        continue;
                    }
                    if (bestLength < length)
                    {
                        bestLength  = length;
                        bestMatcher = matcher;
                    }
                }
            }

            if (bestMatcher == null)
            {
                var message = $"A suitable constructor for type '{instanceType}' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.";
                throw new InvalidOperationException(message);
            }

            return(bestMatcher.CreateInstance(provider));
        }
Esempio n. 2
0
        public object CreateInstance(IServiceProvider services, Type instanceType, params object[] parameters)
        {
            int bestLength = -1;
            ConstructorMatcher bestMatcher = null;

            foreach (var matcher in instanceType
                     .GetTypeInfo()
                     .DeclaredConstructors
                     .Where(c => !c.IsStatic)
                     .Select(constructor => new ConstructorMatcher(constructor)))
            {
                var length = matcher.Match(parameters);
                if (length == -1)
                {
                    continue;
                }
                if (bestLength < length)
                {
                    bestLength  = length;
                    bestMatcher = matcher;
                }
            }

            if (bestMatcher == null)
            {
                throw new Exception(
                          string.Format(
                              "TODO: unable to locate suitable constructor for {0}. " +
                              "Ensure 'instanceType' is concrete and all parameters are accepted by a constructor.",
                              instanceType));
            }

            return(bestMatcher.CreateInstance(services));
        }
Esempio n. 3
0
        /// <summary>
        /// 从服务提供者中创建指定类型与构造参数的实例
        /// </summary>
        /// <param name="provider">服务提供者</param>
        /// <param name="instanceType">指定类型</param>
        /// <param name="parameters">构造参数</param>
        /// <returns></returns>
        public static object CreateInstance(IServiceProvider provider, Type instanceType, params object[] parameters)
        {
            int bestLength = -1;
            ConstructorMatcher bestMatcher = null;

            foreach (ConstructorMatcher matcher in instanceType.GetTypeInfo().DeclaredConstructors.Where(m => !m.IsStatic && m.IsPublic)
                     .Select(m => new ConstructorMatcher(m)))
            {
                int length = matcher.Match(parameters);
                if (length == -1)
                {
                    continue;
                }
                if (bestLength >= length)
                {
                    continue;
                }
                bestLength  = length;
                bestMatcher = matcher;
            }
            if (bestMatcher == null)
            {
                throw new InvalidOperationException(Resources.Ioc_NoConstructorMatch.FormatWith(instanceType));
            }
            return(bestMatcher.CreateInstance(provider));
        }
Esempio n. 4
0
        public object CreateInstance(IServiceProvider services, Type instanceType, params object[] parameters)
        {
            int bestLength = -1;
            ConstructorMatcher bestMatcher = null;

            foreach (var matcher in instanceType
                     .GetTypeInfo()
                     .DeclaredConstructors
                     .Where(c => !c.IsStatic && c.IsPublic)
                     .Select(constructor => new ConstructorMatcher(constructor)))
            {
                var length = matcher.Match(parameters);
                if (length == -1)
                {
                    continue;
                }
                if (bestLength < length)
                {
                    bestLength  = length;
                    bestMatcher = matcher;
                }
            }

            if (bestMatcher == null)
            {
                throw new InvalidOperationException(Resources.FormatNoConstructorMatch(instanceType));
            }

            return(bestMatcher.CreateInstance(services));
        }
        internal static object CreateInstance(IServiceProvider provider, Type instanceType, params object[] parameters)
        {
            int num = -1;
            ConstructorMatcher constructorMatcher = null;

            if (!instanceType.GetTypeInfo().IsAbstract)
            {
                foreach (ConstructorMatcher item in from constructor in instanceType.GetTypeInfo().DeclaredConstructors.Where(delegate(ConstructorInfo c)
                {
                    if (!c.IsStatic)
                    {
                        return(c.IsPublic);
                    }
                    return(false);
                })
                         select new ConstructorMatcher(constructor))
                {
                    int num2 = item.Match(parameters);
                    if (num2 != -1 && num < num2)
                    {
                        num = num2;
                        constructorMatcher = item;
                    }
                }
            }
            if (constructorMatcher == null)
            {
                throw new InvalidOperationException(string.Format("A suitable constructor for type '{0}' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.", instanceType));
            }
            return(constructorMatcher.CreateInstance(provider));
        }
Esempio n. 6
0
        /// <summary>
        /// Instantiate a type with constructor arguments provided directly and/or from an <see cref="IServiceProvider"/>.
        /// </summary>
        /// <param name="provider">The service provider used to resolve dependencies</param>
        /// <param name="instanceType">The type to activate</param>
        /// <param name="parameters">Constructor arguments not provided by the <see cref="provider"/></param>
        /// <returns>An activated object of type instanceType</returns>
        internal static object CreateInstance(IServiceProvider provider, Type instanceType, params object[] parameters)
        {
            int bestLength = -1;
            ConstructorMatcher bestMatcher = null;

            foreach (var matcher in instanceType
                     .GetTypeInfo()
                     .DeclaredConstructors
                     .Where(c => !c.IsStatic && c.IsPublic)
                     .Select(constructor => new ConstructorMatcher(constructor)))
            {
                var length = matcher.Match(parameters);
                if (length == -1)
                {
                    continue;
                }
                if (bestLength < length)
                {
                    bestLength  = length;
                    bestMatcher = matcher;
                }
            }

            if (bestMatcher == null)
            {
                throw new InvalidOperationException($"Unable to locate suitable constructor for type '{instanceType}'.Ensure the type is concrete and all parameters are accepted by a constructor.");
            }

            return(bestMatcher.CreateInstance(provider));
        }
        /// <summary>
        /// 创建实例化。
        /// </summary>
        /// <typeparam name="T">实例化类型。</typeparam>
        /// <param name="provider">服务提供者。</param>
        /// <param name="parameters">不定参数。</param>
        /// <returns>实例化的类型。</returns>
        internal static object CreateInstance(IServiceProvider provider, Type instanceType, params object[] parameters)
        {
            int bestLength = -1;
            ConstructorMatcher bestMatcher = null;

            foreach (var matcher in instanceType
                     .GetTypeInfo()
                     .DeclaredConstructors
                     .Where(c => !c.IsStatic && c.IsPublic)
                     .Select(constructor => new ConstructorMatcher(constructor)))
            {
                var length = matcher.Match(parameters);
                if (length == -1)
                {
                    continue;
                }
                if (bestLength < length)
                {
                    bestLength  = length;
                    bestMatcher = matcher;
                }
            }

            if (bestMatcher == null)
            {
                var message = $"无法找到一个合适的构造函数{instanceType}类型。"
                              + "确保实例化公共构造函数类型的所有参数。";
                throw new InvalidOperationException(message);
            }

            return(bestMatcher.CreateInstance(provider));
        }
Esempio n. 8
0
    public static object CreateInstance(
        IServiceProvider provider,
        [DynamicallyAccessedMembers(ActivatorAccessibility)] Type instanceType,
        params object[] parameters)
    {
        var bestLength = -1;

        ConstructorMatcher bestMatcher = default;

        if (!instanceType.IsAbstract)
        {
            foreach (var constructor in instanceType.GetConstructors())
            {
                var matcher = new ConstructorMatcher(constructor);
                var length  = matcher.Match(parameters);

                if (bestLength < length)
                {
                    bestLength  = length;
                    bestMatcher = matcher;
                }
            }
        }

        if (bestLength == -1)
        {
            var message = $"A suitable constructor for type '{instanceType}' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.";
            throw new InvalidOperationException(message);
        }

        return(bestMatcher.CreateInstance(provider));
    }
Esempio n. 9
0
        /// <summary>
        /// Instantiate a type with constructor arguments provided directly and/or from an <see cref="IServiceProvider"/>.
        /// </summary>
        /// <param name="provider">The service provider used to resolve dependencies</param>
        /// <param name="instanceType">The type to activate</param>
        /// <param name="parameters">Constructor arguments not provided by the <paramref name="provider"/>.</param>
        /// <returns>An activated object of type instanceType</returns>
        public static object CreateInstance(
            IServiceProvider provider,
            [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type instanceType,
            params object[] parameters)
        {
            int  bestLength    = -1;
            bool seenPreferred = false;

            ConstructorMatcher bestMatcher = default;

            if (!instanceType.IsAbstract)
            {
                foreach (ConstructorInfo?constructor in instanceType.GetConstructors())
                {
                    if (!constructor.IsStatic)
                    {
                        var  matcher     = new ConstructorMatcher(constructor);
                        bool isPreferred = constructor.IsDefined(typeof(ActivatorUtilitiesConstructorAttribute), false);
                        int  length      = matcher.Match(parameters);

                        if (isPreferred)
                        {
                            if (seenPreferred)
                            {
                                ThrowMultipleCtorsMarkedWithAttributeException();
                            }

                            if (length == -1)
                            {
                                ThrowMarkedCtorDoesNotTakeAllProvidedArguments();
                            }
                        }

                        if (isPreferred || bestLength < length)
                        {
                            bestLength  = length;
                            bestMatcher = matcher;
                        }

                        seenPreferred |= isPreferred;
                    }
                }
            }

            if (bestLength == -1)
            {
                string?message = $"A suitable constructor for type '{instanceType}' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.";
                throw new InvalidOperationException(message);
            }

            return(bestMatcher.CreateInstance(provider));
        }
Esempio n. 10
0
        /// <summary>
        /// Instantiate a type with constructor arguments provided directly and/or from an <see cref="IServiceProvider"/>.
        /// </summary>
        /// <param name="provider">The service provider used to resolve dependencies</param>
        /// <param name="instanceType">The type to activate</param>
        /// <param name="parameters">Constructor arguments not provided by the <paramref name="provider"/>.</param>
        /// <returns>An activated object of type instanceType</returns>
        public static object CreateInstance(IServiceProvider provider, Type instanceType, params object[] parameters)
        {
            var bestLength    = -1;
            var seenPreferred = false;

            ConstructorMatcher bestMatcher = null;

            if (!instanceType.GetTypeInfo().IsAbstract)
            {
                foreach (var constructor in instanceType
                         .GetTypeInfo()
                         .DeclaredConstructors
                         .Where(c => !c.IsStatic && c.IsPublic))
                {
                    var matcher     = new ConstructorMatcher(constructor);
                    var isPreferred = constructor.IsDefined(typeof(ActivatorUtilitiesConstructorAttribute), false);
                    var length      = matcher.Match(parameters);

                    if (isPreferred)
                    {
                        if (seenPreferred)
                        {
                            ThrowMultipleCtorsMarkedWithAttributeException();
                        }

                        if (length == -1)
                        {
                            ThrowMarkedCtorDoesNotTakeAllProvidedArguments();
                        }
                    }

                    if (isPreferred || bestLength < length)
                    {
                        bestLength  = length;
                        bestMatcher = matcher;
                    }

                    seenPreferred |= isPreferred;
                }
            }

            if (bestMatcher == null)
            {
                var message =
                    $"A suitable constructor for type '{instanceType}' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.";
                throw new InvalidOperationException(message);
            }

            return(bestMatcher.CreateInstance(provider));
        }