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(this IServiceProvider provider, Type instanceType, params object[] parameters)
        {
            int bestLength = -1;

            ConstructorMatcher bestMatcher = default;

            if (!instanceType.GetTypeInfo().IsAbstract)
            {
                foreach (var constructor in instanceType
                         .GetTypeInfo()
                         .DeclaredConstructors)
                {
                    if (!constructor.IsStatic && constructor.IsPublic)
                    {
                        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. 2
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));
    }
        public static ObjectFactory CreateObjectFactory(this Type instanceType, params object[] parameters)
        {
            int bestLength    = -1;
            var seenPreferred = false;

            ConstructorMatcher bestMatcher = default;

            if (!instanceType.GetTypeInfo().IsAbstract)
            {
                foreach (var constructor in instanceType
                         .GetTypeInfo()
                         .DeclaredConstructors)
                {
                    if (!constructor.IsStatic && constructor.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 (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);
            }

            var parameterTypes = bestMatcher.GetParameterTypes();

            return(ActivatorUtilities.CreateFactory(instanceType, parameterTypes));
        }
Esempio n. 4
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));
        }