Esempio n. 1
0
        private object CreateObject(RegisterInfo typeInfo)
        {
            ConstructorInfo ctor     = GetConstructor(typeInfo.TargetType);
            List <object>   paraList = new List <object>();

            switch (typeInfo.LifeTime)
            {
            case LifeTimeType.Transient:
            {
                foreach (ParameterInfo item in ctor.GetParameters())
                {
                    Type         paraType           = item.ParameterType;
                    RegisterInfo targetRegisterInfo = this._Container[paraType.FullName];
                    paraList.Add(CreateObject(targetRegisterInfo));
                }
            }
            break;

            case LifeTimeType.Singleton:
            {
                if (!_SingletonList.ContainsKey(typeInfo.TargetType.FullName))
                {
                    lock (containerLock)
                    {
                        if (_SingletonList.ContainsKey(typeInfo.TargetType.FullName))
                        {
                            return(_SingletonList[typeInfo.TargetType.FullName]);
                        }
                        else
                        {
                            foreach (ParameterInfo item in ctor.GetParameters())
                            {
                                Type         paraType           = item.ParameterType;
                                RegisterInfo targetRegisterInfo = this._Container[paraType.FullName];
                                paraList.Add(CreateObject(targetRegisterInfo));
                            }
                            object oValue = Activator.CreateInstance(typeInfo.TargetType, paraList.ToArray());
                            _SingletonList.Add(typeInfo.TargetType.FullName, oValue);
                        }
                    }
                }
                return(_SingletonList[typeInfo.TargetType.FullName]);
            }

            case LifeTimeType.PerThread:
            {
                object oValue = CallContext.GetData(typeInfo.TargetType.FullName);
                if (oValue == null)
                {
                    foreach (ParameterInfo item in ctor.GetParameters())
                    {
                        Type         paraType           = item.ParameterType;
                        RegisterInfo targetRegisterInfo = this._Container[paraType.FullName];
                        paraList.Add(CreateObject(targetRegisterInfo));
                    }
                    oValue = Activator.CreateInstance(typeInfo.TargetType, paraList.ToArray());
                    CallContext.SetData(typeInfo.TargetType.FullName, oValue);
                }
                return(oValue);
            }

            default:
                break;
            }
            return(Activator.CreateInstance(typeInfo.TargetType, paraList.ToArray()));
        }
Esempio n. 2
0
        public TSource Resolve <TSource>()
        {
            RegisterInfo registerInfo = this._Container[typeof(TSource).FullName];

            return((TSource)this.CreateObject(registerInfo));
        }