Esempio n. 1
0
        public object Inject(Type instanceType, object[] additionalArgs = null)
        {
            var constructors = InstanceExplorer.GetOrderedConstructorList(instanceType);

            foreach (var constructor in constructors)
            {
                var parameters     = InstanceExplorer.GetNonOptionalParameters(constructor);
                var parameterTypes = parameters.Select(p => p.ParameterType);
                var bindings       = _container.Mudule.Bindings.SearchBindings(parameterTypes);

                object[] arguments = null;

                if (additionalArgs != null && parameterTypes.Count() == (bindings.Count() + additionalArgs.Length))
                {
                    arguments = GetParametersImplementations(bindings).Concat(additionalArgs).ToArray();
                    object instance = Create(instanceType, arguments);

                    return(instance);
                }
                else if (parameterTypes.Count() == bindings?.Count())
                {
                    arguments = GetParametersImplementations(bindings).ToArray();
                    object instance = Create(instanceType, arguments);

                    return(instance);
                }
            }

            return(null);
        }
Esempio n. 2
0
 public object Activate(Type instanceType)
 {
     if (InstanceExplorer.HasDefaultConstructor(instanceType))
     {
         return(Activator.CreateInstance(instanceType));
     }
     else
     {
         return(Inject(instanceType));
     }
 }
Esempio n. 3
0
        private object Create(Type instanceType, object[] args)
        {
            object instance = Activator.CreateInstance(instanceType, BindingFlags.CreateInstance |
                                                       BindingFlags.Public | BindingFlags.Instance | BindingFlags.OptionalParamBinding, null, args,
                                                       CultureInfo.CurrentCulture);

            if (InstanceExplorer.TryGetInjectedFields(instance, out List <FieldInfo> injectedFields))
            {
                InjectValues(instance, injectedFields);
            }

            return(instance);
        }