流入中i蒙受的泛型传参难点

ca88会员登录中心
using Autofac;
using IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using TT.Service.Interface;
using TT.Service.Realize;

namespace InjectionServer
{
    public class Dependency
    {

        public static Assembly Assemblys { set; get; } = Assembly.Load("TT.Service");

        static Dependency()
        {
            if (Builder == null)
            {
                Builder = new ContainerBuilder();//初始化容器
                Builder.RegisterAssemblyTypes(Assemblys)
                .Where(o => o != typeof(IRepository<>))
                .AsImplementedInterfaces().PropertiesAutowired().InstancePerDependency();
                InstanceContainer = Builder.Build();
            }
        }

        public Dependency()
        {
            var type = this.GetType();
            foreach (var item in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                try
                {
                    string key = "TT.Service.Interface." + $"I{item.Name.Replace("Example", "")}";
                    Type t = Assemblys.GetType(key);
                    var method = type.GetMethod("GetInstance", BindingFlags.Static | BindingFlags.NonPublic)
                                                  .MakeGenericMethod(t);
                    item.SetValue(this, method.Invoke(null, null));
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }

        /// <summary>
        /// 取出对象管理
        /// </summary>
        internal static IContainer InstanceContainer { get; set; }

        /// <summary>
        /// 注册对象管理
        /// </summary>
        internal static ContainerBuilder Builder { get; set; }

        internal static T GetInstance<T>()
            where T : class
        {
                var instance = InstanceContainer.Resolve<T>();
                return instance;
        }

        internal static void Register<T, K>()
                where T : class
                where K : class
        {
            Builder.RegisterType<T>().As<K>().PropertiesAutowired().InstancePerDependency();
        }

        public IUsers UsersExample { set; get; }

        public ITest TestExample { set; get; }
    }
}

  

 

事先的代码

 1 using Autofac;
 2 using IService;
 3 using System;
 4 using System.Collections.Generic;
 5 using System.Linq;
 6 using System.Reflection;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using TT.Service.Interface;
10 using TT.Service.Realize;
11 
12 namespace InjectionServer
13 {
14     public class Dependency
15     {
16          static Dependency()
17         {
18             if (Builder == null)
19             {
20                 Builder = new ContainerBuilder();
21                 Register();
22                 InstanceContainer = Builder.Build();
23             }
24         }
25 
26         #region 容器管理
27         /// <summary>
28         /// 取出对象管理
29         /// </summary>
30         internal static IContainer InstanceContainer { get; set; }
31 
32         /// <summary>
33         /// 注册对象管理
34         /// </summary>
35         internal static ContainerBuilder Builder { get; set; }
36 
37         /// <summary>
38         /// 获取指定接口的实例
39         /// </summary>
40         /// <typeparam name="T"></typeparam>
41         /// <returns></returns>
42         internal static T GetInstance<T>()
43             where T : class
44         {
45             var instance = InstanceContainer.Resolve<T>();
46             return instance;
47         }
48 
49         /// <summary>
50         /// 注册并且设置为在一个生命周期域中,每一个依赖或调用创建一个单一的共享的实例,且每一个不同的生命周期域,实例是唯一的,不共享的。
51         /// </summary>
52         /// <typeparam name="T"></typeparam>
53         /// <typeparam name="K"></typeparam>
54         /// <param name="t"></param>
55         /// <param name="k"></param>
56         internal static void Register<T, K>()
57              where T : class
58              where K : class
59         {
60             Builder.RegisterType<T>().As<K>().PropertiesAutowired().InstancePerDependency();
61         }
62         #endregion
63 
64         #region 容器注册
65         internal static void Register()
66         {
67 
68             
69             Register<UserService, IUserService>();
70             //Register<UserService, IUserService>();
71             //Register<UserService, IUserService>();
72            
73         }
74         #endregion
75        
76         public IUsers UsersExample {  get { return GetInstance<IUsersService>(); } }
77 
78        
79     }
80 }

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图