app.config配置
实现代码<? xml version="1.0" encoding="utf-8" ?> < configuration > configSections section name ="unity" type ="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration" /> </ unity xmlns ="http://schemas.microsoft.com/practices/2010/unity" > container > register type ="sqlHelp,New_Code" mapTo ="MysqLHelp,New_Code" > lifetime ="singleton" /> register > unity > >
void Main() { IUnityContainer container = new UnityContainer(); UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection( unity "); section.Containers.Default.Configure(container); sqlHelp sqlHelp = container.Resolve<sqlHelp>(); Console.WriteLine(sqlHelp.sqlConnection()); }
运行结果:
my sql connection