我已经为我的应用程序编写了一个自定义配置,但是我没有找到正确的方法来确定 ConfigurationSection 中是否不存在任何 ConfigurationElement。
这是我的自定义配置 app.config 代码
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="ApplicationsSettings" type="App.Configuration.ConfigurationGroup, App.Configuration.Core">
<section name="DataExtractorSettings" type="App.Configuration.DataExtractorConfig, App.Configuration.Core" />
</sectionGroup>
</configSections>
<ApplicationsSettings>
<DataExtractorSettings>
<executionLog>
<enabled value="Y" />
<copyOnReportDirectory value="Y" />
<logFilePath value="D:\MyBatchProcessLog\MasterDataExtractor" />
</executionLog>
<eMail>
<fromEmailID value="gupta@gmail.com" />
<webURL value="http://PROD/login.aspx" />
</eMail>
<!--<parallelProcessing>
<allowed value="Y" />
<threds value="6" />
</parallelProcessing>-->
</DataExtractorSettings>
</ApplicationsSettings>
</configuration>
类 DataExtractorConfig
namespace App.Configuration
{
[SettingProperty(Name: "dataExtractorSettings")]
public class DataExtractorConfig : ConfigurationSection
{
[ConfigurationProperty("executionLog")]
public LogConfig ExecutionLog
{
get
{
if (base["executionLog"] != null)
{
return (LogConfig)base["executionLog"];
}
else
{
return null;
}
}
}
[ConfigurationProperty("parallelProcessing")]
public ParallelProcessConfig ParallelProcessing
{
get
{
if (base["parallelProcessing"] != null)
{
return (ParallelProcessConfig)base["parallelProcessing"];
}
else
{
return null;
}
}
}
[ConfigurationProperty("eMail")]
public EmailConfig Email
{
get
{
if (base["eMail"] != null)
{
return (EmailConfig)base["eMail"];
}
else
{
return null;
}
}
}
}
}
这是从 App.config 文件中读取设置的代码
public static class ConfigurationReader
{
private const string _customSeetingParentNode = "ApplicationsSettings";
public static T GetSettings<T>(string configFileName = "AppSettings.xml", string sectionGroupName = "ApplicationsSettings")
{
System.Configuration.Configuration config;
ConfigurationSection customConfig;
ExeConfigurationFileMap fileMap;
T result;
string settingKey;
try
{
fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = string.Format("{0}\\{1}", AppDomain.CurrentDomain.BaseDirectory, configFileName);
config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
var caSetting = typeof(T).GetCustomAttributes(typeof(SettingProperty), false);
string sectionName;
dynamic dynSettingClass = null;
if (caSetting != null && caSetting.Length > 0)
{
settingKey = (caSetting[0] as SettingProperty).Name;
sectionName = string.Concat(_customSeetingParentNode, "/", settingKey);
customConfig = (ConfigurationSection)config.GetSection(sectionName);
if (customConfig != null && customConfig.ElementInformation.IsPresent)
{
//Here how do I determine the <parallelProcessing> setting is present or not in configuration file,
//like currently it is commented in app config file so the parallelProcessingExists should set with false
//bool parallelProcessingExists = ?
dynSettingClass = customConfig;
}
else
{
dynSettingClass = Activator.CreateInstance(typeof(T));
}
}
else
{
dynSettingClass = Activator.CreateInstance(typeof(T));
}
result = (T)Convert.ChangeType(dynSettingClass, typeof(T));
return result;
}
finally
{
config = null;
customConfig = null;
fileMap = null;
}
}
}
这是从 app.config 文件中读取设置的代码
DataExtractorConfig settigns = ConfigurationReader.GetSettings<DataExtractorConfig>();
如何确定“parallelProcessing”设置是否存在于配置文件中,就像当前它在应用程序配置文件中注释一样,因此 parallelProcessingExists 应该设置为 false。