예제 #1
0
        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);
        }
예제 #2
0
        public bool InjectIntoProperty(PropertyInfo propertyInfo, object container)
        {
            InjectionBinding binding = null;

            if (_Bindings.TryGetValue(propertyInfo.PropertyType, out binding) == true)
            {
                object value = GetInstanceAndInit(binding.InstanceProvider);
                propertyInfo.SetValue(container, value, null);
                return(true);
            }
            return(false);
        }
예제 #3
0
        public bool InjectIntoField(FieldInfo fieldInfo, object container)
        {
            InjectionBinding binding = null;

            if (_Bindings.TryGetValue(fieldInfo.FieldType, out binding) == true)
            {
                object value = GetInstanceAndInit(binding.InstanceProvider);
                fieldInfo.SetValue(container, value);
                return(true);
            }
            return(false);
        }
예제 #4
0
        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);
        }