/// <summary>
        /// 递归--可以完成不限层级的东西
        /// </summary>
        /// <typeparam name="TFrom"></typeparam>
        /// <returns></returns>
        private object ResolveObject(Type abstractType, string shortName = null)
        {
            string key   = this.GetKey(abstractType.FullName, shortName);
            var    model = this.ZhaoxiContainerDictionary[key];

            #region 检测生命周期
            switch (model.Lifetime)
            {
            case LifetimeType.Transient:
                Console.WriteLine("Transient Do Nothing Before...");
                break;

            case LifetimeType.Singleton:
                if (model.SingletonInstance == null)
                {
                    break;
                }
                else
                {
                    return(model.SingletonInstance);
                }

            case LifetimeType.Scope:
                if (this.ZhaoxiContainerScopeDictionary.ContainsKey(key))
                {
                    return(this.ZhaoxiContainerScopeDictionary[key]);
                }
                else
                {
                    break;
                }

            case LifetimeType.PerThread:
                object oValue = CustomCallContext <object> .GetData($"{key}{Thread.CurrentThread.ManagedThreadId}");

                if (oValue == null)
                {
                    break;
                }
                else
                {
                    return(oValue);
                }

            default:
                break;
            }
            #endregion
            Type type = model.TargetType;

            #region  择合适的构造函数
            ConstructorInfo ctor = null;

            //2.选择特性标记的,如果没有则去选择参数最多的
            ctor = type.GetConstructors().FirstOrDefault(c => c.IsDefined(typeof(ZhaoxiConstructorAttribute), true));
            if (ctor == null)
            {
                //1.参数个数最多的
                ctor = type.GetConstructors().OrderByDescending(c => c.GetParameters().Length).First();//参数最多的
            }
            #endregion


            #region 准备构造函数的参数
            List <object> paraList = new List <object>();

            object[] paraConstant = this.ZhaoxiContainerValueDictionary.ContainsKey(key) ? this.ZhaoxiContainerValueDictionary[key]:null;

            int iIndex = 0;
            foreach (var para in ctor.GetParameters())//获取构造函数的参数
            {
                if (para.IsDefined(typeof(ZhaoxiParameterConstantAttribute), true))
                {
                    paraList.Add(paraConstant[iIndex]);
                    iIndex++;
                }
                else
                {
                    Type   paraType      = para.ParameterType;//获取参数的类型 项目中是
                    string paraShortName = this.GetShortName(para);

                    object paraInstance = this.ResolveObject(paraType, paraShortName);
                    paraList.Add(paraInstance);//创建目标类型实例
                }
            }

            #endregion
            object oInstance = Activator.CreateInstance(type, paraList.ToArray());

            #region 属性注入
            foreach (var prop in type.GetProperties().Where(p => p.IsDefined(typeof(ZhaoxiPropertyInjectAttribute), true)))
            {
                Type propType = prop.PropertyType;

                string propShortName = this.GetShortName(prop);

                object propInstance = this.ResolveObject(propType, propShortName);
                prop.SetValue(oInstance, propInstance);
            }
            #endregion

            #region 方法注入
            foreach (var meth in type.GetMethods().Where(m => m.IsDefined(typeof(ZhaoxiMethodAttribute), true)))
            {
                List <object> methParaList = new List <object>();

                //object[] methParaConstant = this.ZhaoxiContainerValueDictionary.ContainsKey(key) ? this.ZhaoxiContainerValueDictionary[key] : null;

                foreach (var methPara in meth.GetParameters())//获取函数的参数
                {
                    //if (methPara.IsDefined(typeof(ZhaoxiParameterConstantAttribute), true))
                    //{
                    //    methParaList.Add(paraConstant[iIndex]);
                    //    iIndex++;
                    //}
                    //else
                    {
                        Type   methParaType     = methPara.ParameterType;//获取参数的类型
                        string paraShortName    = this.GetShortName(methPara);
                        object methParaInstance = this.ResolveObject(methParaType, paraShortName);
                        methParaList.Add(methParaInstance);//创建目标类型实例
                    }
                }
                meth.Invoke(oInstance, methParaList.ToArray());
            }
            #endregion

            #region Lifetime
            switch (model.Lifetime)
            {
            case LifetimeType.Transient:
                Console.WriteLine("Transient Do Nothing After...");
                break;

            case LifetimeType.Singleton:
                model.SingletonInstance = oInstance;
                break;

            case LifetimeType.Scope:
                this.ZhaoxiContainerScopeDictionary[key] = oInstance;
                break;

            case LifetimeType.PerThread:
                CustomCallContext <object> .SetData($"{key}{Thread.CurrentThread .ManagedThreadId}", oInstance);

                break;

            default:
                break;
            }
            #endregion

            return(oInstance.AOP(abstractType));//整合AOP与IOC
            //return oInstance;
        }
Beispiel #2
0
        private object ResolveObject(Type abstracttype, string shortname = null, object[] paramlist = null)
        {
            //string key = abstracttype.FullName;
            string key = GetKey(abstracttype.FullName, shortname);

            if (DIPContainerDic.ContainsKey(key))
            {
                RegisterModel registerModel = DIPContainerDic[key];
                Type          type          = registerModel.ModelType;
                switch (registerModel.LifeType)
                {
                case LifeTimeType.Transient:
                    Console.WriteLine("瞬时实例--前");
                    break;

                case LifeTimeType.Singleton:
                    if (registerModel.SingletonInstance == null)
                    {
                        break;
                    }
                    else
                    {
                        return(registerModel.SingletonInstance);
                    }

                case LifeTimeType.Scoped:
                    if (!DIPContainerScoped.ContainsKey(key) || DIPContainerScoped[key] == null)
                    {
                        break;
                    }
                    else
                    {
                        return(DIPContainerScoped[key]);
                    }

                case LifeTimeType.PerThread:
                    object threadobject = CustomCallContext <object> .GetData($"{key}{Thread.CurrentThread.ManagedThreadId}");

                    Console.WriteLine($"{key}{Thread.CurrentThread.ManagedThreadId}");
                    Console.WriteLine($"threadobject:{threadobject}");
                    if (threadobject == null)
                    {
                        break;
                    }
                    else
                    {
                        return(threadobject);
                    };

                default:
                    break;
                }
                #region 准备构造函数的参数
                //获取指定标记特性的构造函数
                ConstructorInfo ctor = type.GetConstructors().FirstOrDefault(m => m.IsDefined(typeof(DIPContainerAttribute), true));
                //找到构造函数中参数最多的
                if (ctor == null)
                {
                    ctor = type.GetConstructors().OrderByDescending(c => c.GetParameters().Length).First();
                }
                //获取第一个构造函数
                //var ctor = type.GetConstructors()[0];

                List <object> list         = new List <object>();
                object[]      paraConstant = DIPContainerValueDic.ContainsKey(key) ? DIPContainerValueDic[key] : null;
                int           index        = 0;
                foreach (var para in ctor.GetParameters())
                {
                    if (para.IsDefined(typeof(DIPParameterConstantAttribute), true))
                    {
                        list.Add(paraConstant[index++]);
                    }
                    else
                    {
                        Type   paraType       = para.ParameterType;
                        object objectInstance = ResolveObject(paraType);
                        list.Add(objectInstance);
                    }
                    //string paraKey = paraType.FullName;
                    //if (DIPContainerDic.ContainsKey(paraKey))
                    //{
                    //    Type paraTargetType = DIPContainerDic[paraKey];
                    //    list.Add(Activator.CreateInstance(type));
                    //}
                    //else
                    //{
                    //    //DIPContainerDic.Add(paraKey, ()Activator.CreateInstance(type));
                    //}
                }
                #endregion

                object oInstance = Activator.CreateInstance(type, list.ToArray());

                #region 准备构造函数的属性,通过属性标记
                foreach (var property in type.GetProperties().Where(m => m.IsDefined(typeof(DIPPropertyInjectionAttribute), true)))
                {
                    Type   propType      = property.PropertyType;
                    string parashortname = GetShortName(property);
                    object propInstance  = ResolveObject(propType, parashortname);
                    property.SetValue(oInstance, propInstance);
                }
                #endregion

                #region 准备构造函数的方法,通过属性标记
                int methodindex = 0;
                foreach (var method in type.GetMethods().Where(m => m.IsDefined(typeof(DIPMethodInjectionAttribute), true)))
                {
                    List <object>   listmethod     = new List <object>();
                    ParameterInfo[] parameterInfos = method.GetParameters();
                    foreach (var parameterInfo in parameterInfos)
                    {
                        if (parameterInfo.IsDefined(typeof(DIPParameterConstantAttribute), true))
                        {
                            listmethod.Add(paraConstant[methodindex++]);
                        }
                        else
                        {
                            Type   methodType    = parameterInfo.ParameterType;
                            string parashortname = GetShortName(method);

                            object methodInstance = ResolveObject(methodType, parashortname);
                            listmethod.Add(methodInstance);
                        }
                    }

                    method.Invoke(oInstance, listmethod.ToArray());
                }
                #endregion
                switch (registerModel.LifeType)
                {
                case LifeTimeType.Transient:
                    Console.WriteLine("瞬时实例--后");
                    break;

                case LifeTimeType.Singleton:
                    //registerModel.SingletonInstance = oInstance;
                    registerModel.SingletonInstance = ContainerAOPExtend.AOP(oInstance, abstracttype);
                    break;

                case LifeTimeType.Scoped:
                    //DIPContainerScoped[key] = oInstance;
                    DIPContainerScoped[key] = ContainerAOPExtend.AOP(oInstance, abstracttype);
                    break;

                case LifeTimeType.PerThread:
                    CustomCallContext <object> .SetData($"{key}{Thread.CurrentThread.ManagedThreadId}", oInstance);

                    break;

                default:
                    break;
                }

                //return oInstance;
                return(ContainerAOPExtend.AOP(oInstance, abstracttype));
            }
            else
            {
                return(null);
            }
        }