Exemplo n.º 1
0
        object Instantiate(ConstructorInvokeInfo info)
        {
            var constructorParameters = info.ParameterInfos;
            var length         = constructorParameters.Length;
            var parametersList = new object[length];

            for (int i = 0; i < length; i++)
            {
                ParameterInfo parameterInfo = constructorParameters[i];
                object        parameter     = Resolve(parameterInfo.ParameterType);
                if (parameter == null && !parameterInfo.IsOptional)
                {
                    throw new ResolutionException(
                              "Failed to instantiate parameter " + parameterInfo.Name);
                }

                parametersList[i] = parameter;
            }

            var constructorFunc = info.ConstructorFunc;

            try
            {
                return(constructorFunc(parametersList));
            }
            catch (Exception ex)
            {
                throw new ResolutionException("Failed to resolve "
                                              + info.Constructor.DeclaringType, ex);
            }
        }
Exemplo n.º 2
0
        object Instantiate(Type type)
        {
            ConstructorInvokeInfo invokeInfo;

            bool useLock = ThreadSafe;

            if (useLock)
            {
                constructorDictionaryLockSlim.EnterReadLock();
            }

            try
            {
                if (constructorDictionary.TryGetValue(type, out invokeInfo))
                {
                    return(Instantiate(invokeInfo));
                }
            }
            finally
            {
                if (useLock)
                {
                    constructorDictionaryLockSlim.ExitReadLock();
                }
            }

#if NETFX_CORE
            var constructors = type.GetTypeInfo().DeclaredConstructors.Where(x => !x.IsStatic && x.IsPublic).ToArray();
#else
            var constructors = type.GetTypeInfo().DeclaredConstructors;
#endif

            ConstructorInfo constructor = null;

            if (constructors.Any())
            {
                ConstructorInfo bestMatch     = null;
                int             biggestLength = -1;

                foreach (ConstructorInfo constructorInfo in constructors)
                {
                    var dependencyAttributes = constructorInfo.GetCustomAttributes(typeof(InjectDependenciesAttribute), false).ToList();
                    //#if NETSTANDARD || NETFX_CORE
                    //					/* Unfortunately LINQ .Count() is much slower than .Length */
                    //					var attributeCount = dependencyAttributes.Count();
                    //#else
                    //					var attributeCount = dependencyAttributes.Length;
                    //#endif
                    var  attributeCount = dependencyAttributes.Count;
                    bool hasAttribute   = attributeCount > 0;

                    if (hasAttribute)
                    {
                        constructor = constructorInfo;
                        break;
                    }

                    var length = constructorInfo.GetParameters().Length;

                    if (length > biggestLength)
                    {
                        biggestLength = length;
                        bestMatch     = constructorInfo;
                    }
                }

                if (constructor == null)
                {
                    constructor = bestMatch;
                }
            }
            else
            {
                ConstructorInfo bestMatch     = null;
                int             biggestLength = -1;

                constructors = type.GetTypeInfo().DeclaredConstructors.Where(x => !x.IsStatic && x.IsPrivate);

                foreach (ConstructorInfo constructorInfo in constructors)
                {
                    var attributes = constructorInfo.GetCustomAttributes(typeof(InjectDependenciesAttribute), false);

                    if (attributes.Any())
                    {
                        constructor = constructorInfo;
                        break;
                    }

                    var length = constructorInfo.GetParameters().Length;

                    if (length > biggestLength)
                    {
                        biggestLength = length;
                        bestMatch     = constructorInfo;
                    }
                }

                if (constructor == null)
                {
                    constructor = bestMatch;
                }
            }

            if (constructor == null)
            {
                throw new ResolutionException(
                          "Could not locate a constructor for " + type.FullName);
            }

            invokeInfo = new ConstructorInvokeInfo(constructor);

            if (useLock)
            {
                constructorDictionaryLockSlim.EnterWriteLock();
            }

            try
            {
                constructorDictionary[type] = invokeInfo;
            }
            finally
            {
                if (useLock)
                {
                    constructorDictionaryLockSlim.ExitWriteLock();
                }
            }

            return(Instantiate(invokeInfo));
        }