The generic parameter passing problem encountered in autofac injection, autofac
Using Autofac; using IService; 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 (); // initialize container 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> // retrieve object management /// </summary> internal static IContainer InstanceContainer {get; set ;} /// <summary> /// register object management /// </summary> internal static ContainerBuilder {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 ;}}}
Previous Code
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 InjectionServer13 {14 public class Dependency15 {16 static Dependency () 17 {18 if (Builder = null) 19 {20 Builder = new ContainerBuilder (); 21 Register (); 22 InstanceContainer = Builder. build (); 23} 24} 25 26 # region container management 27 // <summary> 28 // retrieve object management 29 /// </summary> 30 internal static IContainer InstanceContainer {get; set;} 31 32 // <summary> 33 // register object management 34 // </summary> 35 internal static ContainerBuilder Builder {get; set ;} 36 37 // <summary> 38 // get the instance 39 of the specified interface // </summary> 40 // <typeparam name = "T"> </typeparam> 41 // <returns> </r Eturns> 42 internal static T GetInstance <T> () 43 where T: class44 {45 var instance = InstanceContainer. resolve <T> (); 46 return instance; 47} 48 49 // <summary> 50 // register and set it to a lifecycle domain, each dependency or call creates a single shared instance, and each different lifecycle domain, the instance is unique and not shared. 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: class58 where K: class59 {60 Builder. registerType <T> (). as <K> (). propertiesAutowired (). instancePerDependency (); 61} 62 # endregion63 64 # region container registration 65 internal static void Register () 66 {67 68 69 Register <UserService, IUserService> (); 70 // Register <UserService, IUserService> (); 71 // Register <UserService, IUserService> (); 72 73} 74 # endregion75 76 public IUsers UsersExample {get {return GetInstance <IUsersService> () ;}} 77 78 79} 80}