Esempio n. 1
0
        public static T CreateProxy <T>(Type realProxyType)
        {
            var  generator = new DynamicProxyGenerator(realProxyType, typeof(T));
            Type type      = generator.GenerateType();

            return((T)Activator.CreateInstance(type));
        }
Esempio n. 2
0
        public static void RegisterDependency()
        {
//            //所有DLL路径
//            var dllPaths =
//                Directory.GetFiles(HttpContext.Current.Server.MapPath("/bin"), "*.dll");
//            //所有类型
//            var typeList =
//                dllPaths.Where(p => p.Contains("Complex.dll")).Select(path =>
//                {
//                    try
//                    {
//                        return Assembly.LoadFrom(path);
//                    }
//                    catch (Exception exp)
//                    {
//#if DEBUG
//                        throw exp;
//#endif
//                        //   Logger.Error(string.Format("IOC注册时,加载程序集[{0}]出错!", path), exp);
//                        return (Assembly)null;
//                    }
//                }

//                    ).Where(a => a != null).Select(a =>
//                    {
//                        try
//                        {
//                            return a.GetTypes();
//                        }
//                        catch (Exception exp)
//                        {
//#if DEBUG
//                            throw exp;
//#endif
//                            //  Logger.Error(string.Format("IOC注册时,获取程序集[{0}]内所有类型时出错!", a.GetName().Name), exp);
//                            return new Type[] { };
//                        }
//                    })
//                    .SelectMany(ts => ts).Where(t => t.Namespace != null && t.Namespace.Contains("Complex.Logical.Realization") && t.IsInterface == false && t.IsAbstract == false);

          

           

            var typeList =
                Assembly.LoadFrom(HttpContext.Current.Server.MapPath("/bin/") + "Complex.dll").GetTypes().Where(t => t.Namespace != null && t.Namespace.Contains("Realization") && t.IsInterface == false && t.IsAbstract == false);   
            //所有ITransientLifetimeManagerRegister类型    

            var ITransientLifetimeManagerRegisterls =
                typeList.Where(
                    t => t.FindInterfaces((tt, o) => o.Equals(tt), typeof(ITransientLifetimeManagerRegister)).Length > 0);   
            //所有IContainerControlledLifetimeManagerRegister类型
            var IContainerControlledLifetimeManagerRegisterls =
                typeList.Where(
                    t => t.FindInterfaces((tt, o) => o.Equals(tt), typeof(IContainerControlledLifetimeManagerRegister)).Length > 0);
            //所有IHierarchicalLifetimeManagerRegister类型
            var IHierarchicalLifetimeManagerRegisterls =
                typeList.Where(
                    t => t.FindInterfaces((tt, o) => o.Equals(tt), typeof(IHierarchicalLifetimeManagerRegister)).Length > 0);
            //所有IExternallyControlledLifetimeManagerRegister类型
            var IExternallyControlledLifetimeManagerRegisterls =
                typeList.Where(
                    t => t.FindInterfaces((tt, o) => o.Equals(tt), typeof(IExternallyControlledLifetimeManagerRegister)).Length > 0);
            //所有IPerThreadLifetimeManagerRegister类型
            var IPerThreadLifetimeManagerRegisterls =
                typeList.Where(
                    t => t.FindInterfaces((tt, o) => o.Equals(tt), typeof(IPerThreadLifetimeManagerRegister)).Length > 0);
            //所有IPerResolveLifetimeManagerRegister类型
            var IPerResolveLifetimeManagerRegisterls =
                typeList.Where(
                    t => t.FindInterfaces((tt, o) => o.Equals(tt), typeof(IPerResolveLifetimeManagerRegister)).Length > 0);

            //注册ITransientLifetimeManagerRegister类型
            foreach (var t in ITransientLifetimeManagerRegisterls)
            {
                var implementInterfaceList = t.FindInterfaces((tt, o) => !o.Equals(tt) && tt.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false)!=null && ((ICO_AOPEnableAttribute)tt.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false)).ICOEnable, typeof(ITransientLifetimeManagerRegister));
                foreach (var iType in implementInterfaceList)
                {

                    ICOConfigAttribute ds = (ICOConfigAttribute) t.GetCustomAttribute(typeof(ICOConfigAttribute), false);
                    ICO_AOPEnableAttribute ia = (ICO_AOPEnableAttribute) iType.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false);

                    if ( ia.AOPEnable )
                    { 
                         
                        var generator = new DynamicProxyGenerator(t, iType);
                        Type type = generator.GenerateType();
                        DependencyUnityContainer.Current.RegisterType(iType, type, ds.Description, new TransientLifetimeManager());
                    }
                    else
                    { 
                        DependencyUnityContainer.Current.RegisterType(iType, t, ds.Description, new TransientLifetimeManager());
                    }
                  
            
                  
                   
                }
            } 
            //注册IContainerControlledLifetimeManagerRegister类型
            foreach (var t in IContainerControlledLifetimeManagerRegisterls)
            {
                var implementInterfaceList = t.FindInterfaces((tt, o) => !o.Equals(tt) && tt.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false) != null && ( (ICO_AOPEnableAttribute) tt.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false) ).ICOEnable, typeof(IContainerControlledLifetimeManagerRegister));
                foreach (var iType in implementInterfaceList)
                {
                    ICOConfigAttribute ds = (ICOConfigAttribute) t.GetCustomAttribute(typeof(ICOConfigAttribute), false);
                    ICO_AOPEnableAttribute ia = (ICO_AOPEnableAttribute) iType.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false);

                    if ( ia.AOPEnable )
                    {
                        var generator = new DynamicProxyGenerator(t, iType);
                        Type type = generator.GenerateType();
                        DependencyUnityContainer.Current.RegisterType(iType, type, ds.Description, new TransientLifetimeManager());
                    }
                    else
                    {
                        
                        DependencyUnityContainer.Current.RegisterType(iType, t, ds.Description, new TransientLifetimeManager());
                    }
                }
            }

            //注册IHierarchicalLifetimeManagerRegister类型
            foreach (var t in IHierarchicalLifetimeManagerRegisterls)
            {
                var implementInterfaceList = t.FindInterfaces((tt, o) => !o.Equals(tt) && tt.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false) != null && ( (ICO_AOPEnableAttribute) tt.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false) ).ICOEnable, typeof(IHierarchicalLifetimeManagerRegister));
                foreach (var iType in implementInterfaceList)
                {
                    ICOConfigAttribute ds = (ICOConfigAttribute) t.GetCustomAttribute(typeof(ICOConfigAttribute), false);
                    ICO_AOPEnableAttribute ia = (ICO_AOPEnableAttribute) iType.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false);

                    if ( ia.AOPEnable )
                    {
                        var generator = new DynamicProxyGenerator(t, iType);
                        Type type = generator.GenerateType();
                        DependencyUnityContainer.Current.RegisterType(iType, type, ds.Description, new TransientLifetimeManager());
                    }
                    else
                    {
                        
                        DependencyUnityContainer.Current.RegisterType(iType, t, ds.Description, new TransientLifetimeManager());
                    }
                }
            }

            //注册IExternallyControlledLifetimeManagerRegister类型
            foreach (var t in IExternallyControlledLifetimeManagerRegisterls)
            {
                var implementInterfaceList = t.FindInterfaces((tt, o) => !o.Equals(tt) && tt.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false) != null && ( (ICO_AOPEnableAttribute) tt.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false) ).ICOEnable, typeof(IExternallyControlledLifetimeManagerRegister));
                foreach (var iType in implementInterfaceList)
                {
                    ICOConfigAttribute ds = (ICOConfigAttribute) t.GetCustomAttribute(typeof(ICOConfigAttribute), false);
                    ICO_AOPEnableAttribute ia = (ICO_AOPEnableAttribute) iType.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false);

                    if ( ia.AOPEnable )
                    {
                        var generator = new DynamicProxyGenerator(t, iType);
                        Type type = generator.GenerateType();
                        DependencyUnityContainer.Current.RegisterType(iType, type, ds.Description, new TransientLifetimeManager());
                    }
                    else
                    {
                       
                        DependencyUnityContainer.Current.RegisterType(iType, t, ds.Description, new TransientLifetimeManager());
                    }
                }
            }
            //注册IPerThreadLifetimeManagerRegister类型
            foreach (var t in IPerThreadLifetimeManagerRegisterls)
            {
                var implementInterfaceList = t.FindInterfaces((tt, o) => !o.Equals(tt) && tt.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false) != null && ( (ICO_AOPEnableAttribute) tt.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false) ).ICOEnable, typeof(IPerThreadLifetimeManagerRegister));
                foreach (var iType in implementInterfaceList)
                {
                    ICOConfigAttribute ds = (ICOConfigAttribute) t.GetCustomAttribute(typeof(ICOConfigAttribute), false);
                    ICO_AOPEnableAttribute ia = (ICO_AOPEnableAttribute) iType.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false);

                    if ( ia.AOPEnable )
                    {
                        var generator = new DynamicProxyGenerator(t, iType);
                        Type type = generator.GenerateType();
                        DependencyUnityContainer.Current.RegisterType(iType, type, ds.Description, new TransientLifetimeManager());
                    }
                    else
                    {
                       
                        DependencyUnityContainer.Current.RegisterType(iType, t, ds.Description, new TransientLifetimeManager());
                    }
                }
            }
            //注册IPerResolveLifetimeManagerRegister类型
            foreach (var t in IPerResolveLifetimeManagerRegisterls)
            {
                var implementInterfaceList = t.FindInterfaces((tt, o) => !o.Equals(tt) && tt.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false) != null && ( (ICO_AOPEnableAttribute) tt.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false) ).ICOEnable, typeof(IPerResolveLifetimeManagerRegister));
                foreach (var iType in implementInterfaceList)
                {
                    ICOConfigAttribute ds = (ICOConfigAttribute) t.GetCustomAttribute(typeof(ICOConfigAttribute), false);
                    ICO_AOPEnableAttribute ia = (ICO_AOPEnableAttribute) iType.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false);

                    if ( ia.AOPEnable )
                    {
                        var generator = new DynamicProxyGenerator(t, iType);
                        Type type = generator.GenerateType();
                        DependencyUnityContainer.Current.RegisterType(iType, type, ds.Description, new TransientLifetimeManager());
                    }
                    else
                    {
                       
                        DependencyUnityContainer.Current.RegisterType(iType, t, ds.Description, new TransientLifetimeManager());
                    }
                }
            }


        }