Ejemplo n.º 1
0
 public InjectionBinding ToInstance(object instance)
 {
     BoundTo = instance.GetType();
     // just use the instance
     GetInstance = (constructorInfo, constructorArgs) => instance;
     // prevent injection, since this is a singleton
     DoFieldAndPropertyInjection = false;
     return(this);
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Define that the requested type is bound to a target type.
 /// Every injection results in a new instance of the target type.
 /// </summary>
 /// <typeparam name="TType"></typeparam>
 /// <returns></returns>
 public InjectionBinding To <TType>()
 {
     BoundTo = typeof(TType);
     // create the instance by invoking the construtor
     GetInstance = (constructorInfo, constructorArgs) => constructorInfo.Invoke(constructorArgs);
     // injection of fields and properties should happen, since we have a newly created instance
     DoFieldAndPropertyInjection = true;
     return(this);
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Instantiates the target type.
        /// </summary>
        /// <param name="type">The target type</param>
        /// <param name="bindings">All binding definitions to be used</param>
        /// <param name="invoker">Function that is used to create the instance</param>
        /// <param name="doInjection">True if field- and property injections should be applied</param>
        /// <returns>The target type instance</returns>
        private object InstantiateInternal(Type type, List <InjectionBinding> bindings, InstantiateFunc invoker, bool doInjection)
        {
            object instance = null;

            // search for constructor injection
            var constructors = type.GetConstructors();

            var constructorParams = new ArrayList();

            // look for a constructor that fits
            // TODO: we use the first constructor that is defined on the target type, does this work in every case?
            foreach (var constructor in constructors)
            {
                // only use constructors that where really defined on the class and not on base classes
                if (constructor.DeclaringType == constructor.ReflectedType)
                {
                    var parameters = constructor.GetParameters();

                    // only do the injection if we shall do it (f.e. for singleton instance injection we will not do it)
                    if (doInjection)
                    {
                        // check all constructor parameters
                        foreach (var parameter in parameters)
                        {
                            // create an instance of the parameter type
                            var constructorParameter = Instantiate(parameter.ParameterType, bindings);
                            constructorParams.Add(constructorParameter);
                        }
                    }

                    // instantiate the first constructor found
                    instance = invoker.Invoke(constructor, constructorParams.ToArray());

                    // we use the first constructor that fits
                    break;
                }
            }

            // only do the injection if we shall do it (f.e. for singleton instance injection we will not do it)
            if (doInjection)
            {
                Inject(instance, bindings);
            }

            return(instance);
        }