public class Container { /// <summary> /// IOC容器 /// </summary> public static IContainer container = null; public static T Resolve<T>() { try { if (container == null) { Initialise(); } } catch (Exception ex) { throw new Exception("IOC实例化出错!" + ex.Message); } return container.Resolve<T>(); } public static void Initialise() { var builder = new ContainerBuilder();//InstancePerLifetimeScope //builder.RegisterType< typeof(T).As<IT>().InstancePerLifetimeScope(); builder.RegisterType<LoginDAl>().As<ILoginDAL>().InstancePerLifetimeScope(); container = builder.Build(); } }
调用的时候
private ILoginDAL dal =Container.Resolve<ILoginDAL>();
这样调用也可以构造函数调用
|
请发表评论