Exemplo n.º 1
0
        public void InstantiateBindings()
        {
            foreach (IList <Binding> bindingList in binder.Values)
            {
                foreach (Binding binding in bindingList)
                {
                    this.Get(binding.Key, binding.Name, false);
                }
            }
            if (exceptions.Count != 0)
            {
                IList <string> previousString  = new List <string>();
                string         exceptionString = "Bindings not found for the following:\n";

                for (int i = 0; i < exceptions.Count; i++)
                {
                    BindingNotFoundException bindingNotFoundException = exceptions[i];
                    if (!previousString.Contains(bindingNotFoundException.Message))
                    {
                        previousString.Add(bindingNotFoundException.Message);
                        exceptionString += bindingNotFoundException.Message + "\n";
                        for (int x = i; x < exceptions.Count; x++)
                        {
                            BindingNotFoundException nextException = exceptions[x];
                            if (bindingNotFoundException.Message.Equals(nextException.Message))
                            {
                                if (nextException.BaseClass != null)
                                {
                                    exceptionString += "\tin class " + nextException.BaseClass + "\n";
                                }
                            }
                        }
                    }
                }
                throw new BindingNotFoundException(exceptionString);
            }
            //Initialize commands
            foreach (KeyValuePair <Type, IList <Type> > pairs in signalsToCommands)
            {
                Signal s = (Signal)this.Get(pairs.Key, "", true);
                foreach (Type commands in pairs.Value)
                {
                    s.AddCommand(commands);
                }
            }
        }
Exemplo n.º 2
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);
                }
            }
        }