Exemplo n.º 1
0
        public void ActivateWithValue()
        {
            object value = BINDING_VALUE;

            cachedBinding = new CachedBinding(typeof(TestModel), value);

            Assert.AreEqual(BINDING_VALUE, cachedBinding.Activate());
        }
Exemplo n.º 2
0
        public void ActivateWithNoParamsTest()
        {
            cachedBinding = new CachedBinding(typeof(TestModel));
            testModel     = (TestModel)cachedBinding.Activate();

            Assert.AreEqual(DEFAULT_MESSAGE, testModel.Message);
            Assert.AreEqual(DEFAULT_NUM, testModel.Num);
        }
Exemplo n.º 3
0
        public void ActivateWithParamsTest()
        {
            object[] paramArray = new object[1];
            paramArray[0] = PARAM_MESSAGE;
            cachedBinding = new CachedBinding(typeof(TestModel), paramArray, "");
            testModel     = (TestModel)cachedBinding.Activate();

            Assert.AreEqual(PARAM_MESSAGE, testModel.Message);
            Assert.AreEqual(DEFAULT_NUM, testModel.Num);
        }
Exemplo n.º 4
0
 private void AddToCachedBinder(Type keyClazz, CachedBinding cachedBinding)
 {
     if (cachedBinder.ContainsKey(keyClazz))
     {
         cachedBinder[keyClazz].Add(cachedBinding);
     }
     else
     {
         IList <CachedBinding> list = new List <CachedBinding>();
         list.Add(cachedBinding);
         cachedBinder.Add(keyClazz, list);
     }
 }
Exemplo n.º 5
0
        private object GetInstanceAndCache(Type keyClazz, CachedBinding cachedBinding, bool throwExceptions = false)
        {
            AddToCachedBinder(keyClazz, cachedBinding);

            object noParamInstance = cachedBinding.Activate();

            //UnityEngine.Debug.Log (cachedBinding.Value.GetType ());
            //if (cachedBinding.Value.GetType()
            //{
            SetMemberInfo(noParamInstance.GetType().GetProperties(), typeof(PropertyInfo), ref noParamInstance, throwExceptions);
            SetMemberInfo(noParamInstance.GetType().GetFields(), typeof(FieldInfo), ref noParamInstance, throwExceptions);
            //}

            if (cachedBinding.Singleton)
            {
                singletons.Add(cachedBinding.Type.ToString() + "_" + cachedBinding.Name, noParamInstance);
                noParamInstance = singletons[cachedBinding.Type.ToString() + "_" + cachedBinding.Name];
            }

            return(noParamInstance);
        }
Exemplo n.º 6
0
        private object Get(Type keyClazz, string name = "", bool throwExceptions = true)
        {
            if (keyClazz.Equals(typeof(IBinder)))
            {
                return(this);
            }
            try
            {
                //Check to see if the instance is cached
                try
                {
                    return(GetCachedBinding(keyClazz, name, throwExceptions));
                }
                catch (Exception)
                {
                }

                IList <Binding> bindings;
                try
                {
                    bindings = binder[keyClazz];
                }
                catch (KeyNotFoundException ex)
                {
                    if (!keyClazz.IsInterface)
                    {
                        bindings = new List <Binding>();
                        Binding bin = new Binding(keyClazz);
                        bin.Value = keyClazz;
                        bindings.Add(bin);
                        binder[keyClazz] = bindings;
                    }
                    else
                    {
                        throw ex;
                    }
                }

                foreach (Binding namedBinding in bindings)
                {
                    if (namedBinding.Name.Equals(name))
                    {
                        Type type = namedBinding.Value as Type;
                        if (type == null)
                        {
                            //return namedBinding.Value;
                            CachedBinding b = new CachedBinding(keyClazz, namedBinding.Value, name);
                            //AddToCachedBinder(keyClazz, b);
                            return(b.Activate());
                        }
                        if (Convert.GetTypeCode(namedBinding.Value) != TypeCode.Object)
                        {
                            CachedBinding b = new CachedBinding(type, namedBinding.Value, name);
                            AddToCachedBinder(keyClazz, b);
                            return(b.Activate());
                        }
                        //Reflect the constructor
                        ConstructorInfo[] constructorInfoArray = type.GetConstructors();
                        foreach (ConstructorInfo constructorInfo in constructorInfoArray)
                        {
                            foreach (Attribute attr in constructorInfo.GetCustomAttributes(false))
                            {
                                if (attr is Inject)
                                {
                                    ParameterInfo[] parameterInfoArray = constructorInfo.GetParameters();
                                    object[]        paramArray         = new object[parameterInfoArray.Length];
                                    int             i = 0;
                                    foreach (ParameterInfo parameterInfo in parameterInfoArray)
                                    {
                                        Type   parameterType = parameterInfo.ParameterType;
                                        string parameterName = "";
                                        foreach (Attribute parameterAttrs in parameterInfo.GetCustomAttributes(false))
                                        {
                                            if (parameterAttrs is Named)
                                            {
                                                Named namedAttr = parameterAttrs as Named;
                                                parameterName = namedAttr.Name;
                                            }
                                        }
                                        paramArray[i] = this.Get(parameterType, parameterName, throwExceptions);
                                        if (paramArray[i] == null)
                                        {
                                            exceptions[exceptions.Count - 1].BaseClass = type;
                                        }
                                        i++;
                                    }

                                    //Cache and return the instance
                                    CachedBinding cachedBinding = new CachedBinding(type, paramArray, name);
                                    cachedBinding.Singleton = namedBinding.Singleton;

                                    return(GetInstanceAndCache(keyClazz, cachedBinding, throwExceptions));
                                }
                            }
                        }
                        //Cache and return the instance
                        CachedBinding cached = new CachedBinding(type, name);
                        cached.Singleton = namedBinding.Singleton;

                        return(GetInstanceAndCache(keyClazz, cached, throwExceptions));
                    }
                }
                throw new Exception();
            }
            catch (Exception)
            {
                //Set-up the exception
                BindingNotFoundException bindingNotFoundException;
                if (name.Equals(""))
                {
                    bindingNotFoundException = new BindingNotFoundException("Binding not found for " + keyClazz.ToString() + ".");
                }
                else
                {
                    bindingNotFoundException = new BindingNotFoundException("Binding named " + name + " not found for " + keyClazz.ToString() + ".");
                }
                if (throwExceptions)
                {
                    throw bindingNotFoundException;
                }
                else
                {
                    exceptions.Add(bindingNotFoundException);
                    return(null);
                }
            }
        }
Exemplo n.º 7
0
 public void TearDown()
 {
     cachedBinding = null;
     testModel     = null;
 }