Exemplo n.º 1
0
        override protected void resolver(IBinding binding)
        {
            IInjectionBinding iBinding = binding as IInjectionBinding;

            object [] supply = iBinding.GetSupply();

            if (supply != null)
            {
                foreach (object a in supply)
                {
                    Type aType = a as Type;
                    if (suppliers.ContainsKey(aType) == false)
                    {
                        suppliers[aType] = new Dictionary <Type, IInjectionBinding>();
                    }
                    object[] keys = iBinding.key as object[];
                    foreach (object key in keys)
                    {
                        Type keyType = key as Type;
                        if (suppliers[aType].ContainsKey(keyType as Type) == false)
                        {
                            suppliers[aType][keyType] = iBinding;
                        }
                    }
                }
            }

            base.resolver(binding);
        }
Exemplo n.º 2
0
        private void armorAgainstInfiniteLoops(IInjectionBinding binding)
        {
            if (binding == null)
            {
                return;
            }

            if (_infinityLock == null)
            {
                _infinityLock = new Dictionary <IInjectionBinding, int>();
            }

            if (_infinityLock.ContainsKey(binding) == false)
            {
                _infinityLock.Add(binding, 0);
            }

            _infinityLock[binding] = _infinityLock[binding] + 1;
            if (_infinityLock[binding] > InfinityLimit)
            {
                var type = "Undefined";
                if (binding.value is Type toType)
                {
                    type = toType.Name;
                }

                throw new InjectionException(
                          "There appears to be a circular dependency. Terminating loop. " + type,
                          InjectionExceptionType.CIRCULAR_DEPENDENCY);
            }
        }
Exemplo n.º 3
0
        private void Bind(ImplicitBindingVO toBind)
        {
            //We do not check for the existence of a binding. Because implicit bindings are weak bindings, they are overridden automatically by other implicit bindings
            //Therefore, ImplementedBy will be overriden by an Implements to that interface.

            IInjectionBinding binding = injectionBinder.Bind(toBind.BindTypes.First());

            binding.Weak();            //SDM2014-0120: added as part of cross-context implicit binding fix (moved from below)

            for (int i = 1; i < toBind.BindTypes.Count; i++)
            {
                Type bindType = toBind.BindTypes.ElementAt(i);
                binding.Bind(bindType);
            }

            binding = toBind.ToType != null?
                      binding.To(toBind.ToType).ToName(toBind.Name).ToSingleton() :
                          binding.ToName(toBind.Name).ToSingleton();

            if (toBind.IsCrossContext)             //Bind this to the cross context injector
            {
                binding.CrossContext();
            }

            //binding.Weak();//SDM2014-0120: removed as part of cross-context implicit binding fix (moved up higher)
        }
Exemplo n.º 4
0
        public void TestGetBindingAbstract()
        {
            binder.Bind <ISimpleInterface> ().To <ClassWithConstructorParameters> ();
            IInjectionBinding binding = binder.GetBinding <ISimpleInterface> () as IInjectionBinding;

            Assert.IsNotNull(binding);
        }
Exemplo n.º 5
0
        public T GetInstance <T>()
        {
            _isBindingCompleted = true;

            Type bindingType = typeof(T);

            object            value   = null;
            IInjectionBinding 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);
        }
Exemplo n.º 6
0
        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));
            }
        }
Exemplo n.º 7
0
        public void TestGetNamedBinding()
        {
            binder.Bind <ISimpleInterface> ().To <ClassWithConstructorParameters> ().ToName <MarkerClass>();
            IInjectionBinding binding = binder.GetBinding <ISimpleInterface> (typeof(MarkerClass)) as IInjectionBinding;

            Assert.IsNotNull(binding);
        }
Exemplo n.º 8
0
 /// Generate a Singleton instance
 protected object singletonOf(IInjectionBinding binding, object[] args)
 {
     if (binding.value != null)
     {
         if (binding.value.GetType().IsInstanceOfType(typeof(Type)))
         {
             object o = createFromValue(binding.value, args);
             if (o == null)
             {
                 return(null);
             }
             binding.SetValue(o);
         }
         else
         {
             //no-op. We already have a binding value!
         }
     }
     else
     {
         object generateImplicitValue = generateImplicit((binding.key as object[])[0], args);
         if (generateImplicitValue != null)
         {
             binding.SetValue(generateImplicitValue);
         }
         else
         {
             UnityEngine.Debug.LogError("IInjectionBinding generateImplicit object == NULL");
         }
     }
     return(binding.value);
 }
Exemplo n.º 9
0
        public object Get(IInjectionBinding binding, object[] args)
        {
            if (binding == null)
            {
                throw new InjectionException("InjectorFactory cannot act on null binding", InjectionExceptionType.NULL_BINDING);
            }
            InjectionBindingType type = binding.type;

            if (objectMap.ContainsKey(binding) == false)
            {
                objectMap [binding] = new Dictionary <object, object> ();
            }

            switch (type)
            {
            case InjectionBindingType.SINGLETON:
                return(singletonOf(binding, args));

            case InjectionBindingType.VALUE:
                return(valueOf(binding));

            default:
                break;
            }
            return(instanceOf(binding, args));
        }
Exemplo n.º 10
0
        private void DestroyInjectedValue(object instance, IInjectionBinding binding, IInjectionInfo injection)
        {
            var value = injection.PropertyInfo.GetValue(instance);

            switch (binding.BindingType)
            {
            case InjectionBindingType.InstanceProvider when binding.InjectionMode == InjectionMode.Factory:
                // Provider constructed (if configured) on the first instance inject.
                // Provider is destroyed when it's binding is destroyed.
                ((IInjectionProvider)binding.Value).ReturnInstance(value);
                return;

            case InjectionBindingType.Value when binding.InjectionMode == InjectionMode.Singleton:
                // Value is constructed (if configured) on the first instance inject.
                // Value is destroyed when it's binding is destroyed.
                return;

            case InjectionBindingType.Type when binding.InjectionMode == InjectionMode.Factory:
                // Factory instance is destroyed when it's un injected.
                Destroy(value, true);
                return;

            case InjectionBindingType.Type when binding.InjectionMode == InjectionMode.Singleton:
                // Singleton must stay alive even if all dependent parts un injected it.
                // Singleton will be destroyed when it's binding is destroyed.
                return;

            default:
                throw new InjectionException(InjectionExceptionType.ValueNotDestroyed);
            }
        }
Exemplo n.º 11
0
        private object getValueInjection        /*
                                                 * Copyright 2013 ThirdMotion, Inc.
                                                 *
                                                 *	Licensed under the Apache License, Version 2.0 (the "License");
                                                 *	you may not use this file except in compliance with the License.
                                                 *	You may obtain a copy of the License at
                                                 *
                                                 *		http://www.apache.org/licenses/LICENSE-2.0
                                                 *
                                                 *		Unless required by applicable law or agreed to in writing, software
                                                 *		distributed under the License is distributed on an "AS IS" BASIS,
                                                 *		WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
                                                 *		See the License for the specific language governing permissions and
                                                 *		limitations under the License.
                                                 */

            (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 && !binding.toInject)
            {
                return(binding.value);
            }
            else
            {
                return(Instantiate(binding));
            }
        }
Exemplo n.º 12
0
        public void TestRescanDoesNotOverrideCrossContextValue()
        {
            object mockGameObject = new object();
            TestImplicitBindingContext context = new TestImplicitBindingContext(mockGameObject);

            //Get our binding. It should have value of Runtime Type. It hasn't been instantiated yet.
            IInjectionBinding binding = context.injectionBinder.GetBinding <TestImplicitBindingClass>();

            Assert.IsTrue(binding.value is Type);

            //GetInstance. This should set the value to the instantiated class
            TestImplicitBindingClass instanceValue = context.injectionBinder.GetInstance <TestImplicitBindingClass>();

            Assert.IsNotNull(instanceValue);
            IInjectionBinding bindingAfterGetInstance = context.injectionBinder.GetBinding <TestImplicitBindingClass>();

            Assert.IsTrue(bindingAfterGetInstance.value is TestImplicitBindingClass);
            Assert.AreSame(bindingAfterGetInstance, binding);

            //Rescan our implicit bindings
            //Our binding value should remain
            context.Scan();
            IInjectionBinding bindingAfterRescan = context.injectionBinder.GetBinding <TestImplicitBindingClass>();

            Assert.AreSame(bindingAfterRescan, binding);             //Should be the same binding, and not override it

            Assert.IsTrue(bindingAfterRescan.value is TestImplicitBindingClass);
            Assert.AreSame(bindingAfterRescan.value, instanceValue);
        }
Exemplo n.º 13
0
        public void TestGetBindingFlat()
        {
            binder.Bind <InjectableSuperClass> ().To <InjectableSuperClass> ();
            IInjectionBinding binding = binder.GetBinding <InjectableSuperClass> () as IInjectionBinding;

            Assert.IsNotNull(binding);
        }
Exemplo n.º 14
0
 public void Bind(IInjectionBinding binding)
 {
     if (_bindings.ContainsKey(binding.Key))
     {
         throw new InjectionException(InjectionExceptionType.BindingAlreadyRegistered, binding.Key.FullName);
     }
     _bindings.Add(binding.Key, binding);
 }
 protected override IInjector GetInjectorForBinding(IInjectionBinding binding)
 {
     if (binding.isCrossContext && CrossContextBinder != null)
     {
         return(CrossContextBinder.injector);
     }
     return(injector);
 }
Exemplo n.º 16
0
        public object Instantiate(IInjectionBinding binding, bool tryInjectHere)
        {
            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)
            {
                var 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.
            {
                var reflection = reflector.Get(reflectionType);

#if STRANGE_ANALYSIS
                strange.extensions.analysis.impl.StrangeDebugger.Measure(
                    () =>
                {
                    var args = GetArgs(reflection);
                    retv     = _factory.Get(binding, args);
                }, null, reflectionType?.Name, "ConstructorInjection");
#else
                var args = GetArgs(reflection);
                retv = _factory.Get(binding, args);
#endif
                if (tryInjectHere)
                {
                    TryInject(binding, retv);
                }
            }

            _infinityLock =
                null; //Clear our infinity lock so the next time we instantiate we don't consider this a circular dependency

            return(retv);
        }
Exemplo n.º 17
0
 public void Unbind(IInjectionBinding binding)
 {
     if (!_bindings.TryGetValue(binding.Key, out var bindingAdded) || binding != bindingAdded)
     {
         throw new InjectionException(InjectionExceptionType.BindingDoesntMatch, binding);
     }
     _bindings.Remove(binding.Key);
     DestroyBinding(binding);
 }
Exemplo n.º 18
0
        /// <summary>
        /// 绑定值类型到接口
        /// </summary>
        /// <typeparam name="I">接口类型</typeparam>
        /// <typeparam name="V">值类型</typeparam>
        /// <param name="crossContext">是否跨域</param>
        protected void BindValue <I, V>(bool crossContext = false)
        {
            IInjectionBinding binding = injectionBinder.Bind <I>().To <V>().ToSingleton();

            if (crossContext)
            {
                binding.CrossContext();
            }
        }
Exemplo n.º 19
0
        public static IInjectionBinding BindType <T> (Type type, object name = null)
        {
            IInjectionBinding binding = Context.injectionBinder.Bind <T> ().To(type).ToSingleton();

            if (name != null)
            {
                binding.ToName(name);
            }
            return(binding);
        }
Exemplo n.º 20
0
        public void Unsupply(Type injectionType, Type targetType)
        {
            IInjectionBinding binding = GetSupplier(injectionType, targetType);

            if (binding != null)
            {
                suppliers [targetType].Remove(injectionType);
                binding.Unsupply(targetType);
            }
        }
Exemplo n.º 21
0
        protected object instanceOf(IInjectionBinding binding, object[] args)
        {
            if (binding.value != null)
            {
                return(createFromValue(binding.value, args));
            }
            object value = generateImplicit((binding.key as object[])[0], args);

            return(createFromValue(value, args));
        }
Exemplo n.º 22
0
        public static IInjectionBinding BindValue <T> (T value, object name = null)
        {
            IInjectionBinding binding = Context.injectionBinder.Bind <T> ().ToValue(value);

            if (name != null)
            {
                binding.ToName(name);
            }
            return(binding);
        }
Exemplo n.º 23
0
        /// Gets an instance of the provided generic type and name from the InjectionBinder
        /// Always bear in mind that doing this risks adding
        /// dependencies that must be cleaned up when Contexts
        /// are removed.
        override public object GetComponent <T>(object name)
        {
            IInjectionBinding binding = injectionBinder.GetBinding <T>(name);

            if (binding != null)
            {
                return(injectionBinder.GetInstance <T>(name));
            }
            return(null);
        }
Exemplo n.º 24
0
        private object GetInjectionValue(object instance, IInjectionBinding binding, IInjectionInfo injectionInfo)
        {
            switch (binding.BindingType)
            {
            case InjectionBindingType.InstanceProvider when binding.InjectionMode == InjectionMode.Factory:
            {
                var instanceProvider = binding.Value is Type type
                                               ? (IInjectionProvider)Activator.CreateInstance(type)
                                               : (IInjectionProvider)binding.Value;

                if (binding.ToConstruct && !CheckIsConstructed(instanceProvider))
                {
                    Construct(instanceProvider, true);
                    binding.SetValue(instanceProvider);
                    MarkConstructed(binding.Value);
                }

                return(instanceProvider.TakeInstance(instance, injectionInfo.Attribute));
            }

            case InjectionBindingType.Value when binding.InjectionMode == InjectionMode.Singleton:
            {
                if (binding.ToConstruct && !CheckIsConstructed(binding.Value))
                {
                    Construct(binding.Value, true);
                    MarkConstructed(binding.Value);
                }

                return(binding.Value);
            }

            case InjectionBindingType.Type when binding.InjectionMode == InjectionMode.Factory:
            {
                var value = Activator.CreateInstance((Type)binding.Value);
                Construct(value, true);
                return(value);
            }

            case InjectionBindingType.Type when binding.InjectionMode == InjectionMode.Singleton:
            {
                if (binding.ToConstruct && !CheckIsConstructed(binding.Value))
                {
                    var value = Activator.CreateInstance((Type)binding.Value);
                    Construct(value, true);
                    binding.SetValue(value);
                    MarkConstructed(binding.Value);
                }

                return(binding.Value);
            }

            default:
                throw new InjectionException(InjectionExceptionType.ValueNotProvided);
            }
        }
Exemplo n.º 25
0
    public static IInjectionBinding ReBind <T>(this ICrossContextInjectionBinder _this)
    {
        IInjectionBinding binding = _this.GetBinding <T>();

        if (binding != null)
        {
            _this.CrossContextBinder.Unbind <T>();
        }

        return(_this.Bind <T>());
    }
Exemplo n.º 26
0
    public void BindCrossContext(object key)
    {
        IInjectionBinding binding = injectionBinder.GetBinding(key);

        if (binding == null)
        {
            throw new Exception("Object " + key.ToString() + " not yet bound, cannot make cross context");
        }

        binding.CrossContext();
    }
Exemplo n.º 27
0
        /// Generate a Singleton instance
        protected object singletonOf(IInjectionBinding binding, object[] args)
        {
            object name = binding.name;
            Dictionary <object, object> dict = objectMap [binding];

            if (dict.ContainsKey(name) == false)
            {
                dict [name] = createFromValue(binding.value, args);
            }
            return(dict[name]);
        }
Exemplo n.º 28
0
        public void TestSingletonUnbind()
        {
            Parent.injectionBinder.Bind <TestModel>().ToSingleton().CrossContext();            //bind it once here and it should be accessible everywhere

            TestModel parentModel = Parent.injectionBinder.GetInstance <TestModel>() as TestModel;

            Assert.IsNotNull(parentModel);

            TestModel childOneModel = ChildOne.injectionBinder.GetInstance <TestModel>() as TestModel;

            Assert.IsNotNull(childOneModel);
            TestModel childTwoModel = ChildTwo.injectionBinder.GetInstance <TestModel>() as TestModel;

            Assert.IsNotNull(childTwoModel);

            //Lots of lines that tell us they're all the same binding

            Assert.AreSame(parentModel, childOneModel);
            Assert.AreSame(parentModel, childTwoModel);
            Assert.AreSame(childOneModel, childTwoModel);

            IInjectionBinding binding = Parent.injectionBinder.GetBinding <TestModel>();

            Assert.IsNotNull(binding);
            Assert.IsTrue(binding.isCrossContext);

            IInjectionBinding childBinding = ChildOne.injectionBinder.GetBinding <TestModel>();

            Assert.IsNotNull(childBinding);
            Assert.IsTrue(childBinding.isCrossContext);


            Assert.AreEqual(0, parentModel.Value);

            parentModel.Value++;
            Assert.AreEqual(1, childOneModel.Value);

            parentModel.Value++;
            Assert.AreEqual(2, childTwoModel.Value);



            //unbinding the parent should unbind the children who do not have a specific local binding

            Parent.injectionBinder.Unbind <TestModel>();

            binding = Parent.injectionBinder.GetBinding <TestModel>();
            Assert.IsNull(binding);
            childBinding = ChildOne.injectionBinder.GetBinding <TestModel>();
            Assert.IsNull(childBinding);
            childBinding = ChildTwo.injectionBinder.GetBinding <TestModel>();
            Assert.IsNull(childBinding);
        }
Exemplo n.º 29
0
        public virtual object GetInstance(Type key, object name)
        {
            IInjectionBinding binding = GetBinding(key, name) as IInjectionBinding;

            if (binding == null)
            {
                throw new InjectionException("InjectionBinder has no binding for:\n\tkey: " + key + "\nname: " + name, InjectionExceptionType.NULL_BINDING);
            }
            object instance = GetInjectorForBinding(binding).Instantiate(binding);

            return(instance);
        }
Exemplo n.º 30
0
        /*
         * Instances.
         */

        public object GetInstance(IInjectionBinding binding, object callerInstance, IInjectionInfo injectionInfo)
        {
            if (binding == null)
            {
                throw new InjectionException(InjectionExceptionType.BindingIsMissing, callerInstance);
            }
            IncrementDependencyCounter(binding, callerInstance);
            var instance = GetInjectionValue(callerInstance, binding, injectionInfo);

            DecrementDependencyCounter(binding);
            return(instance);
        }
Exemplo n.º 31
0
		public object Instantiate(IInjectionBinding binding, bool tryInjectHere)
		{
			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], reflectionType, null);
				}
				retv = factory.Get (binding, args);

				if (tryInjectHere)
				{
					TryInject(binding, retv);
				}
			}
			infinityLock = null; //Clear our infinity lock so the next time we instantiate we don't consider this a circular dependency

			return retv;
		}
		/// Generate a Singleton instance
		protected object singletonOf(IInjectionBinding binding, object[] args)
		{
			if (binding.value != null)
			{
				if (binding.value.GetType().IsInstanceOfType(typeof(Type)))
				{
					binding.SetValue(createFromValue(binding.value, args));
				}
				else
				{
					//no-op. We already have a binding value!
				}
			}
			else
			{
				binding.SetValue(generateImplicit((binding.key as object[])[0], args));
			}
			return binding.value;
		}
Exemplo n.º 33
0
		public object Get(IInjectionBinding binding, object[] args)
		{
			if (binding == null)
			{
				throw new InjectionException ("InjectorFactory cannot act on null binding", InjectionExceptionType.NULL_BINDING);
			}
			InjectionBindingType type = binding.type;

			switch (type)
			{
				case InjectionBindingType.SINGLETON:
					return singletonOf (binding, args);
				case InjectionBindingType.VALUE:
					return valueOf (binding);
				default:
					break;
			}

			return instanceOf (binding, args);
		}
Exemplo n.º 34
0
		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;
		}
Exemplo n.º 35
0
        public object Get(IInjectionBinding binding, object[] args)
        {
            if (binding == null)
            {
                throw new InjectionException ("InjectorFactory cannot act on null binding", InjectionExceptionType.NULL_BINDING);
            }
            InjectionBindingType type = binding.type;
            if (objectMap.ContainsKey(binding) == false)
            {
                objectMap [binding] = new Dictionary<object, object> ();
            }

            switch (type)
            {
                case InjectionBindingType.SINGLETON:
                    return singletonOf (binding, args);
                case InjectionBindingType.VALUE:
                    return valueOf (binding);
                default:
                    break;
            }
            return instanceOf (binding, args);
        }
Exemplo n.º 36
0
        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;
        }
Exemplo n.º 37
0
        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;
        }
Exemplo n.º 38
0
 /// Generate a Singleton instance
 protected object singletonOf(IInjectionBinding binding, object[] args)
 {
     object name = binding.name;
     Dictionary<object, object> dict = objectMap [binding];
     if (dict.ContainsKey(name) == false)
     {
         dict [name] = createFromValue(binding.value, args);
     }
     return dict[name];
 }
Exemplo n.º 39
0
		/// The binding already has a value. Simply return it.
		protected object valueOf(IInjectionBinding binding)
		{
			return binding.value;
		}
Exemplo n.º 40
0
		public object Get(IInjectionBinding binding)
		{
			return Get (binding, null);
		}
Exemplo n.º 41
0
 /// Generate a Singleton instance
 protected object singletonOf(IInjectionBinding binding, object[] args)
 {
     if (binding.value != null)
     {
     #if NETFX_CORE
         if (chk.IsInstanceOfType(typeof(Type), binding.value))
     #else
         if (binding.value.GetType().IsInstanceOfType(typeof(Type)))
     #endif
         {
             object o = createFromValue (binding.value, args);
             if (o == null)
                 return null;
             binding.SetValue(o);
         }
         else
         {
             //no-op. We already have a binding value!
         }
     }
     else
     {
         binding.SetValue(generateImplicit((binding.key as object[])[0], args));
     }
     return binding.value;
 }
Exemplo n.º 42
0
		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;
		}
Exemplo n.º 43
0
 /// Generate a Singleton instance
 protected object singletonOf(IInjectionBinding binding, object[] args)
 {
     object name = binding.name;
     Dictionary<object, object> dict = objectMap [binding];
     if (dict.ContainsKey(name) == false)
     {
         if (binding.value != null)
         {
             dict[name] = createFromValue(binding.value, args);
         }
         else
         {
             dict[name] = generateImplicit((binding.key as object[])[0], args);
         }
     }
     return dict[name];
 }
Exemplo n.º 44
0
		/// Generate a new instance
		protected object instanceOf(IInjectionBinding binding, object[] args)
		{
			if (binding.value != null)
			{
				return createFromValue(binding.value, args);
			}
			object value = generateImplicit ((binding.key as object[]) [0], args);
			return createFromValue(value, args);
		}
Exemplo n.º 45
0
 private void armorAgainstInfiniteLoops(IInjectionBinding binding)
 {
     if (binding == null)
     {
         return;
     }
     if (infinityLock == null)
     {
         infinityLock = new Dictionary<IInjectionBinding, int> ();
     }
     if(infinityLock.ContainsKey(binding) == false)
     {
         infinityLock.Add (binding, 0);
     }
     infinityLock [binding] = infinityLock [binding] + 1;
     if (infinityLock [binding] > INFINITY_LIMIT)
     {
         throw new InjectionException ("There appears to be a circular dependency. Terminating loop.", InjectionExceptionType.CIRCULAR_DEPENDENCY);
     }
 }
Exemplo n.º 46
0
 /// Generate a new instance
 protected object instanceOf(IInjectionBinding binding, object[] args)
 {
     return createFromValue(binding.value, args);
 }