Unity的外部配置文件使用方法

前一阵用这个,在Google里一搜,很多人都说文档里没有这个方法,不幸被我找到了,以免大家继续重复发明轮子>_<

Using Alternative Configuration Sources

You can also use any XML configuration file or other source of configuration information if required. For example, you can load configuration information into containers from a specific named configuration file by using the .NET Framework System.Configuration.Configuration class to retrieve the information from any XML formatted file. The following code shows how you can read configuration information from a file named MyConfig.config.

C#
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename 
= "MyConfig.config";
System.Configuration.Configuration config 
  
= ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None); 
UnityConfigurationSection section
  
= (UnityConfigurationSection)config.GetSection("unity");
IUnityContainer container 
= new UnityContainer();
section.Containers[
"myContainer"].Configure(container);

VB
Dim parentContainer As IUnityContainer = New UnityContainer()
Dim childContainer As IUnityContainer =  parentContainer.CreateChildContainer()
Dim section As UnityConfigurationSection _
  
= CType(ConfigurationManager.GetSection("unity"), UnityConfigurationSection)
section.Containers(
"containerOne").GetConfigCommand().Configure(parentContainer)
section.Containers(
"nestedChildContainer").Configure(childContainer) 

转载于:https://www.cnblogs.com/HCOONa/archive/2009/03/16/1413718.html

你可能感兴趣的:(Unity的外部配置文件使用方法)