public T GetInstance <T>() { _IsBindingCompleted = true; Type bindingType = typeof(T); object value = null; InjectionBinding binding = null; if (_Bindings.TryGetValue(bindingType, out binding) == true) { value = GetInstanceAndInit(binding.InstanceProvider); } else { // Handler error InjectionError error = CreateError(InjectionErrorType.CanNotFindBindingForType, bindingType, null, 1); if (_ShouldThrowException) { throw new InjectionException(error.Error, error.Message); } } return((T)value); }
public void InjectInto(object container, IMemberInjector injectionOverride = null) { _IsBindingCompleted = true; // Get reflection container for object. Will be performed once per type ReflectionCache classReflection = GetReflection(container.GetType()); // Inject into fields foreach (FieldInfo fieldInfo in classReflection.Fields) { if (injectionOverride != null && injectionOverride.InjectIntoField(fieldInfo, container)) { continue; } else if (InjectIntoField(fieldInfo, container)) { continue; } else { // Handler error InjectionError error = CreateError(InjectionErrorType.CanNotFindBindingForType, fieldInfo.FieldType, null, 1); if (_ShouldThrowException) { throw new InjectionException(error.Error, error.Message); } continue; } } // Inject into properties foreach (PropertyInfo propertyInfo in classReflection.Properties) { if (injectionOverride != null && injectionOverride.InjectIntoProperty(propertyInfo, container)) { continue; } else if (InjectIntoProperty(propertyInfo, container)) { continue; } else { // Handler error InjectionError error = CreateError(InjectionErrorType.CanNotFindBindingForType, propertyInfo.PropertyType, null, 1); if (_ShouldThrowException) { throw new InjectionException(error.Error, error.Message); } continue; } } }
private bool GetBinding(Type bindingType, out IInjectionBinding binding) { if (_bindings.TryGetValue(bindingType, out binding) == false) { // Handler error InjectionError error = CreateError(InjectionErrorType.CanNotFindBindingForType, bindingType, null, "", 2); if (_shouldThrowException) { throw new InjectionException(error.error, error.message); } return(false); } return(true); }
private InjectionError CreateError(InjectionErrorType errorType, Type bindingType = null, Type providerType = null, int callerLevel = 0) { string callerInfo = GetCallerInfo(1 + callerLevel); string bindingTypeAsString = (bindingType != null)?(bindingType.ToString()):(""); string providerTypeAsString = (providerType != null)?(providerType.ToString()):(""); object[] args = new object[] { callerInfo, bindingTypeAsString, providerTypeAsString }; string errorMessage = String.Format(_ErrorMessages[(int)errorType], args); InjectionError error = new InjectionError(errorType, errorMessage); _Errors.Add(error); return(error); }
public IInstanceProvider AddTypedProvider <T>(Type bindingType) where T : new() { Type providerType = typeof(T); // Check if type T is assignable to target type if (!bindingType.IsAssignableFrom(providerType)) { // Handler error InjectionError error = CreateError(InjectionErrorType.TypeNotAssignableToTarget, bindingType, providerType, 2); if (_ShouldThrowException) { throw new InjectionException(error.Error, error.Message); } return(null); } // Check if a provider with given type exist IInstanceProvider provider; if (_Providers.TryGetValue(providerType, out provider)) { // Check if existing provider is same with requested one if (provider.GetType() != typeof(NewInstanceProvider <T>)) { // Handler error InjectionError error = CreateError(InjectionErrorType.AlreadyAddedTypeWithDifferentProvider, bindingType, providerType, 2); if (_ShouldThrowException) { throw new InjectionException(error.Error, error.Message); } } } else { provider = new NewInstanceProvider <T>(); _Providers.Add(providerType, provider); } return(provider); }
public IInstanceProvider AddValueProvider(Type bindingType, object value) { Type providerType = value.GetType(); // Check if type of value is assignable to target type if (!bindingType.IsAssignableFrom(providerType)) { // Handler error InjectionError error = CreateError(InjectionErrorType.ValueNotAssignableToBindingType, bindingType, providerType, "", 2); if (_shouldThrowException) { throw new InjectionException(error.error, error.message); } return(null); } // Check if a provider with given type exist IInstanceProvider provider; if (_providers.TryGetValue(providerType, out provider)) { // Check if existing provider is same with requested one if (provider.GetType() != typeof(SingleInstanceProvider)) { // Handler error InjectionError error = CreateError(InjectionErrorType.AlreadyAddedTypeWithDifferentProvider, bindingType, providerType, "", 2); if (_shouldThrowException) { throw new InjectionException(error.error, error.message); } } } else { provider = new SingleInstanceProvider(value); _providers.Add(providerType, provider); } return(provider); }
private bool CheckRestrictions(object container, IInjectionBinding binding) { List <IInjectionRestriction> restrictions = binding.RestrictionList; for (int i = 0; i < restrictions.Count; i++) { IInjectionRestriction restriction = restrictions[i]; bool restrictionResult = restrictions[i].Check(container, binding.BindingType, binding.InstanceProvider); if (restrictionResult == false) { // Handler error InjectionError error = CreateError(InjectionErrorType.InjectionRestricted, binding.BindingType, null, restriction.GetInfo(), 2); if (_shouldThrowException) { throw new InjectionException(error.error, error.message); } return(false); } } return(true); }
public IInstanceProviderSetter AddBinding <T>() { Type bindingType = typeof(T); InjectionBinding binding = null; if (!_IsBindingCompleted) { // Check is there is an existing binding with given type if (_Bindings.TryGetValue(bindingType, out binding)) { // Handler error InjectionError error = CreateError(InjectionErrorType.AlreadyAddedBindingForType, bindingType, null, 1); if (_ShouldThrowException) { throw new InjectionException(error.Error, error.Message); } } else { // Add binding binding = new InjectionBinding(bindingType, this); _Bindings.Add(bindingType, binding); } } else { // Handler error InjectionError error = CreateError(InjectionErrorType.BindingAfterInjection, bindingType, null, 1); if (_ShouldThrowException) { throw new InjectionException(error.Error, error.Message); } } return(binding); }