1006 与dll(assembly)相关的配置(config)文件的读取

1:需求:
在由多个dll(Assembly)构成的系统中,ConfigurationManager默认读取的是主程序的app.config文件, 因此,新的模块创建完成以后,把模块相关的配置与主程序exe配置文件进行合并,此步骤难免出现纰漏(多了,少了,重复了),因此需要创建一个与某个dll(Assembly)相关的配置文件读取类, 这个类的目的是规避配置文件合并动作,需要注意:这样做带来的负作用是配置文件很多,搞不清楚在哪配置。个人认为,分开利大于弊。

2:实现代码下载【密码v51v]

 

参考代码:

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace FXPRJ.T1000.CMN.Infrastructure.EntitiesAndUtilities.Utilities
{
public class FxConfigurationManager
{
private Dictionary<string, FxConfiguration> _configurations = new Dictionary<string, FxConfiguration>();
private static FxConfigurationManager _singleton_instance;
public static FxConfigurationManager SingletonInstance
{
get
{
if (_singleton_instance == null)
_singleton_instance = new FxConfigurationManager();
return _singleton_instance;
}
}
public FxConfiguration this[Assembly assembly]
{
get
{
if (assembly==null) return new FxConfiguration(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None));
string assemblyPath = assembly.Location;
string assemblyConfigPath = assemblyPath + ".config";
string assemblyName = assembly.GetName().Name;
FxConfiguration config = null;
if (_configurations.ContainsKey(assemblyName))
{
//do nothing
}
else
{
ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
configMap.ExeConfigFilename = assemblyConfigPath;
if (File.Exists(assemblyConfigPath))
{
config = new FxConfiguration(ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None));
}
else
{
config = new FxConfiguration(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None));
}

_configurations.Add(assemblyName, config);
}
config = _configurations[assemblyName];
return config;
}
}
}
}

内部需要的类:

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Linq;
using System.Text;

namespace FXPRJ.T1000.CMN.Infrastructure.EntitiesAndUtilities.Utilities
{
public class FxConfiguration
{
private Configuration _config = null;
public Configuration CurrConfiguration
{
get
{
return _config;
}
}
private FxConfiguration()
{

}
public FxConfiguration(Configuration config)
{
if (config != null )
{
AppSettingsSection appSettings = config.AppSettings;
_appSettings.Clear();
if (appSettings != null)
{
foreach (string key in appSettings.Settings.AllKeys)
{
string value = appSettings.Settings[key].Value;
_appSettings.Add(key, value);
}
}
ConnectionStringsSection connStrings = config.ConnectionStrings;
if (connStrings != null)
{
_connectionStringSettingsCollection = connStrings.ConnectionStrings;
}
_config = config;
}
}

private NameValueCollection _appSettings = new NameValueCollection();
public NameValueCollection AppSettings
{
get
{
return _appSettings;
}
}
private ConnectionStringSettingsCollection _connectionStringSettingsCollection = new ConnectionStringSettingsCollection();
public ConnectionStringSettingsCollection ConnectionStrings
{
get
{
return _connectionStringSettingsCollection;
}
}
}
}

测试代码:

Assembly assembly = typeof(Class1).Assembly;
string ss = FxConfigurationManager.SingletonInstance[assembly].AppSettings["Test"];
string conn = FxConfigurationManager.SingletonInstance[assembly].ConnectionStrings["DBCONN"].ConnectionString;

 

posted @ 2018-11-26 20:46  mp3down3  阅读(157)  评论(0编辑  收藏  举报