如何确定 "ConfigurationElement" 在 "ConfigurationSection" 自定义配置设置上可用
How to determine "ConfigurationElement" is available on "ConfigurationSection" custom configuration settings
我已经为我的应用程序编写了自定义配置,但我没有找到确定 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>
Class 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。
您创建了 DataExtractorConfig
class 让我们使用它。从 GetSection
方法返回的对象可以转换为 DataExtractorConfig
。现在您可以轻松访问 ParallelProcessing
、Email
、ExecutionLog
属性并检查是否存在相应的配置部分。例如:
var customConfig = (DataExtractorConfig)config.GetSection(sectionName);
if (customConfig != null && customConfig.ElementInformation.IsPresent)
{
if(customConfig.ParallelProcessing.ElementInformation.IsPresent)
{
// TODO
}
else
{
// TODO
}
dynSettingClass = customConfig;
}
我已经为我的应用程序编写了自定义配置,但我没有找到确定 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>
Class 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。
您创建了 DataExtractorConfig
class 让我们使用它。从 GetSection
方法返回的对象可以转换为 DataExtractorConfig
。现在您可以轻松访问 ParallelProcessing
、Email
、ExecutionLog
属性并检查是否存在相应的配置部分。例如:
var customConfig = (DataExtractorConfig)config.GetSection(sectionName);
if (customConfig != null && customConfig.ElementInformation.IsPresent)
{
if(customConfig.ParallelProcessing.ElementInformation.IsPresent)
{
// TODO
}
else
{
// TODO
}
dynSettingClass = customConfig;
}