gpt4 book ai didi

c# - 如何确定 "ConfigurationElement"在 "ConfigurationSection"自定义配置设置上可用

转载 作者:行者123 更新时间:2023-11-30 14:48:15 24 4
gpt4 key购买 nike

我已经为我的应用程序编写了自定义配置,但我没有找到正确的方法来确定是否有任何 ConfigurationElement 不存在于 ConfigurationSection 中。

这是我的自定义配置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。

最佳答案

您创建了 DataExtractorConfig 类,让我们使用它。从 GetSection 方法返回的对象可以转换为 DataExtractorConfig。现在您可以轻松访问 ParallelProcessingEmailExecutionLog 属性并检查是否存在相应的配置部分。例如:

var customConfig = (DataExtractorConfig)config.GetSection(sectionName);

if (customConfig != null && customConfig.ElementInformation.IsPresent)
{
if(customConfig.ParallelProcessing.ElementInformation.IsPresent)
{
// TODO
}
else
{
// TODO
}

dynSettingClass = customConfig;
}

关于c# - 如何确定 "ConfigurationElement"在 "ConfigurationSection"自定义配置设置上可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42291355/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com