首页 > 代码库 > autofac的小知识点

autofac的小知识点

autofac 注入中i遇到的泛型传参问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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 { setget; } = 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(nullnull));
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
 
        /// <summary>
        /// 取出对象管理
        /// </summary>
        internal static IContainer InstanceContainer { getset; }
 
        /// <summary>
        /// 注册对象管理
        /// </summary>
        internal static ContainerBuilder Builder { getset; }
 
        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 { setget; }
 
        public ITest TestExample { setget; }
    }
}

  

 

之前的代码

 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 }

 

autofac的小知识点