private object CreateObject(Implementation imp)
        {
            if (singletons.ContainsKey(imp.TImplementation))
            {
                return(singletons[imp.TImplementation]);
            }
            else
            {
                ConstructorInfo ctor       = imp.TImplementation.GetConstructors().First();
                ParameterInfo[] ctorParams = ctor.GetParameters();
                object[]        invokeArgs = new object[ctorParams.Length];

                for (int i = 0; i < ctorParams.Length; i++)
                {
                    if (ctorParams[i].ParameterType.IsValueType)
                    {
                        throw new Exception("Implementation constructor takes invalid parameters.");
                    }

                    //Check for constructor attribute
                    DependencyKeyAttribute atr = ctorParams[i].GetCustomAttribute <DependencyKeyAttribute>();
                    if (atr != null)
                    {
                        invokeArgs[i] = Resolve(ctorParams[i].ParameterType, Convert.ToInt32(atr.Name));
                    }
                    else
                    {
                        invokeArgs[i] = Resolve(ctorParams[i].ParameterType);
                    }
                }

                object res = Activator.CreateInstance(imp.TImplementation, invokeArgs);

                if (imp.LifeCycle == LifeCycle.Singleton)
                {
                    return(singletons.TryAdd(imp.TImplementation, res) ? res : singletons[imp.TImplementation]);
                }

                return(res);
            }
        }
Example #2
0
        object CreateImplemetationInstance(Type type)
        {
            ConstructorInfo[] constructors = type.GetConstructors();
            var bestConstructor            = constructors[0];

            foreach (var constructor in constructors)
            {
                if (constructor.GetParameters().Length > bestConstructor.GetParameters().Length)
                {
                    bestConstructor = constructor;
                }
            }


            List <object> parameters = new List <object>();

            foreach (ParameterInfo parameter in bestConstructor.GetParameters())
            {
                if (dependencies.ContainsKey(parameter.ParameterType))
                {
                    DependencyKeyAttribute dependencyAttribute =
                        (DependencyKeyAttribute)parameter.GetCustomAttribute(typeof(DependencyKeyAttribute));
                    if (dependencyAttribute != null)
                    {
                        parameters.Add(Resolver(parameter.ParameterType, dependencyAttribute.ImplementationName));
                    }
                    else
                    {
                        parameters.Add(Resolver(parameter.ParameterType));
                    }
                }
                else
                {
                    parameters.Add(null);
                }
            }

            return(Activator.CreateInstance(type, parameters.ToArray()));
        }