private object getValueInjection(Type t, object name, object target) { IInjectionBinding binding = binder.GetBinding(t, name); failIf(binding == null, "Attempt to Instantiate a null binding.", InjectionExceptionType.NULL_BINDING, t, name, target); if (binding.type == InjectionBindingType.VALUE) { if (!binding.toInject) { return(binding.value); } else { object retv = Inject(binding.value, false); binding.ToInject(false); return(retv); } } else if (binding.type == InjectionBindingType.SINGLETON) { if (binding.value is Type || binding.value == null) { Instantiate(binding); } return(binding.value); } else { return(Instantiate(binding)); } }
public object TryInject(IInjectionBinding binding, object target) { //If the InjectorFactory returns null, just return it. Otherwise inject the retv if it needs it //This could happen if Activator.CreateInstance returns null if (target != null) { if (binding.toInject) { target = Inject(target, binding.IsAvoidDestroy); } if (binding.type == InjectionBindingType.SINGLETON || binding.type == InjectionBindingType.VALUE) { binding.ToInject(false); } } return(target); }
public object Instantiate(IInjectionBinding binding) { failIf(binder == null, "Attempt to instantiate from Injector without a Binder", InjectionExceptionType.NO_BINDER); failIf(factory == null, "Attempt to inject into Injector without a Factory", InjectionExceptionType.NO_FACTORY); armorAgainstInfiniteLoops(binding); object retv = factory.Get(binding); //Factory can return null in the case that there are no parameterless constructors. //In this case, the following routine attempts to generate based on a preferred constructor if (retv == null) { IReflectedClass reflection = reflector.Get(binding.value as Type); Type[] parameters = reflection.constructorParameters; int aa = parameters.Length; object[] args = new object [aa]; for (int a = 0; a < aa; a++) { args [a] = getValueInjection(parameters[a] as Type, null, retv); } retv = factory.Get(binding, args); if (retv == null) { return(null); } retv = Inject(retv, false); } else if (binding.toInject) { retv = Inject(retv, binding.type != InjectionBindingType.VALUE); if (binding.type == InjectionBindingType.SINGLETON || binding.type == InjectionBindingType.VALUE) { //prevent double-injection binding.ToInject(false); } } infinityLock = null; return(retv); }
private object getValueInjection(Type t, object name, object target, PropertyInfo propertyInfo) { IInjectionBinding suppliedBinding = null; if (target != null) { suppliedBinding = binder.GetSupplier(t, target is Type ? target as Type : target.GetType()); } IInjectionBinding binding = suppliedBinding ?? binder.GetBinding(t, name); failIf(binding == null, "Attempt to Instantiate a null binding", InjectionExceptionType.NULL_BINDING, t, name, target, propertyInfo); if (binding.type == InjectionBindingType.VALUE) { if (!binding.toInject) { return(binding.value); } else { object retv = Inject(binding.value, false); binding.ToInject(false); return(retv); } } else if (binding.type == InjectionBindingType.SINGLETON) { if (binding.value is Type || binding.value == null) { Instantiate(binding, true); } return(binding.value); } else { return(Instantiate(binding, true)); } }
public object Instantiate(IInjectionBinding binding) { failIf(binder == null, "Attempt to instantiate from Injector without a Binder", InjectionExceptionType.NO_BINDER); failIf(factory == null, "Attempt to inject into Injector without a Factory", InjectionExceptionType.NO_FACTORY); armorAgainstInfiniteLoops (binding); object retv = null; Type reflectionType = null; if (binding.value is Type) { reflectionType = binding.value as Type; } else if (binding.value == null) { object[] tl = binding.key as object[]; reflectionType = tl [0] as Type; if (reflectionType.IsPrimitive || reflectionType == typeof(Decimal) || reflectionType == typeof(string)) { retv = binding.value; } } else { retv = binding.value; } if (retv == null) //If we don't have an existing value, go ahead and create one. { IReflectedClass reflection = reflector.Get (reflectionType); Type[] parameterTypes = reflection.constructorParameters; object[] parameterNames = reflection.ConstructorParameterNames; int aa = parameterTypes.Length; object[] args = new object [aa]; for (int a = 0; a < aa; a++) { args [a] = getValueInjection (parameterTypes[a] as Type, parameterNames[a], null); } retv = factory.Get (binding, args); //If the InjectorFactory returns null, just return it. Otherwise inject the retv if it needs it //This could happen if Activator.CreateInstance returns null if (retv != null) { if (binding.toInject) { retv = Inject (retv, false); } if (binding.type == InjectionBindingType.SINGLETON || binding.type == InjectionBindingType.VALUE) { //prevent double-injection binding.ToInject(false); } } } infinityLock = null; //Clear our infinity lock so the next time we instantiate we don't consider this a circular dependency return retv; }
public object Instantiate(IInjectionBinding binding) { failIf(binder == null, "Attempt to instantiate from Injector without a Binder", InjectionExceptionType.NO_BINDER); failIf(factory == null, "Attempt to inject into Injector without a Factory", InjectionExceptionType.NO_FACTORY); armorAgainstInfiniteLoops (binding); object retv = factory.Get (binding); //Factory can return null in the case that there are no parameterless constructors. //In this case, the following routine attempts to generate based on a preferred constructor if (retv == null) { IReflectedClass reflection = reflector.Get (binding.value as Type); Type[] parameters = reflection.constructorParameters; int aa = parameters.Length; object[] args = new object [aa]; for (int a = 0; a < aa; a++) { args [a] = getValueInjection (parameters[a] as Type, null, retv); } retv = factory.Get (binding, args); if (retv == null) { return null; } retv = Inject (retv, false); } else if (binding.toInject) { retv = Inject (retv, true); if (binding.type == InjectionBindingType.SINGLETON || binding.type == InjectionBindingType.VALUE) { //prevent double-injection binding.ToInject(false); } } infinityLock = null; return retv; }
public object Instantiate(IInjectionBinding binding) { failIf(binder == null, "Attempt to instantiate from Injector without a Binder", InjectionExceptionType.NO_BINDER); failIf(factory == null, "Attempt to inject into Injector without a Factory", InjectionExceptionType.NO_FACTORY); armorAgainstInfiniteLoops(binding); object retv = null; Type reflectionType = null; if (binding.value is Type) { reflectionType = binding.value as Type; } else if (binding.value == null) { object[] tl = binding.key as object[]; reflectionType = tl [0] as Type; if (reflectionType.IsPrimitive || reflectionType == typeof(Decimal) || reflectionType == typeof(string)) { retv = binding.value; } } else { retv = binding.value; } if (retv == null) //If we don't have an existing value, go ahead and create one. { IReflectedClass reflection = reflector.Get(reflectionType); Type[] parameters = reflection.constructorParameters; int aa = parameters.Length; object[] args = new object [aa]; for (int a = 0; a < aa; a++) { args [a] = getValueInjection(parameters[a] as Type, null, null); } retv = factory.Get(binding, args); //If the InjectorFactory returns null, just return it. Otherwise inject the retv if it needs it //This could happen if Activator.CreateInstance returns null if (retv != null) { if (binding.toInject) { retv = Inject(retv, false); } if (binding.type == InjectionBindingType.SINGLETON || binding.type == InjectionBindingType.VALUE) { //prevent double-injection binding.ToInject(false); } } } infinityLock = null; //Clear our infinity lock so the next time we instantiate we don't consider this a circular dependency return(retv); }
public object TryInject(IInjectionBinding binding, object target) { //If the InjectorFactory returns null, just return it. Otherwise inject the retv if it needs it //This could happen if Activator.CreateInstance returns null if (target != null) { if (binding.toInject) { target = Inject(target, false); } if (binding.type == InjectionBindingType.SINGLETON || binding.type == InjectionBindingType.VALUE) { //prevent double-injection binding.ToInject(false); } } return target; }
public object Instantiate(IInjectionBinding binding) { failIf(binder == null, "Attempt to instantiate from Injector without a Binder", InjectionExceptionType.NO_BINDER); failIf(factory == null, "Attempt to inject into Injector without a Factory", InjectionExceptionType.NO_FACTORY); armorAgainstInfiniteLoops (binding); object retv; Type reflectionType; if (binding.value is Type) { reflectionType = binding.value as Type; } else if (binding.value == null) { object[] tl = binding.key as object[]; reflectionType = tl [0] as Type; if (reflectionType.IsPrimitive || reflectionType == typeof(Decimal) || reflectionType == typeof(string)) { return binding.value; } } else { return binding.value; } IReflectedClass reflection = reflector.Get (reflectionType); Type[] parameters = reflection.constructorParameters; int aa = parameters.Length; object[] args = new object [aa]; for (int a = 0; a < aa; a++) { args [a] = getValueInjection (parameters[a] as Type, null, null); } retv = factory.Get (binding, args); if (retv == null) { return null; } if (binding.toInject) { retv = Inject (retv, false); } infinityLock = null; if (binding.type == InjectionBindingType.SINGLETON || binding.type == InjectionBindingType.VALUE) { //prevent double-injection binding.ToInject(false); } return retv; }