作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
考虑以下配置部分:
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="xxx.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
</sectionGroup>
</configSections>
<applicationSettings>
<xxx.Properties.Settings>
<setting name="xxx_Service1" serializeAs="String">
<value>https://xxx.service1</value>
</setting>
<setting name="xxx_Service1" serializeAs="String">
<value>https://xxx.service2</value>
</setting>
<setting name="xxx_Service1" serializeAs="String">
<value>https://xxx.service3</value>
</setting>
</xxx.Properties.Settings>
</applicationSettings>
我尝试实现 System.Configuration.ClientSettingsSection 的自定义版本。我需要完全相同的层次结构
ConfigSection
- ConfigElement
- ConfigElement
- ConfigElement
基于 AssemblyExplorer
中的代码,我编写了自定义实现:
{
static void Main()
{
var sec = (CustomClientSettingsSection) ConfigurationManager.GetSection("serviceSettings");
Console.Read();
}
}
public class CustomClientSettingsSection : ConfigurationSection
{
private static readonly ConfigurationProperty _propSettings = new ConfigurationProperty((string)null,
typeof(CustomSettingElementCollection), (object)null, ConfigurationPropertyOptions.IsDefaultCollection);
[ConfigurationProperty("", IsDefaultCollection = true)]
public CustomSettingElementCollection Services
{
get { return (CustomSettingElementCollection)this[CustomClientSettingsSection._propSettings]; }
}
}
public class CustomSettingElementCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new CustomSettingElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((CustomSettingElement)element).Key;
}
}
public class CustomSettingElement : ConfigurationElement
{
private static readonly ConfigurationProperty _propName = new ConfigurationProperty("name", typeof(string), (object)"", ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsKey);
internal string Key
{
get
{
return this.Name;
}
}
[ConfigurationProperty("name", DefaultValue = "", IsKey = true, IsRequired = true)]
public string Name
{
get
{
return (string)this[CustomSettingElement._propName];
}
set
{
this[CustomSettingElement._propName] = (object)value;
}
}
}
我正在尝试解析这个config.xml
<configSections>
<section name="serviceSettings" type="ConsoleApplication1.CustomClientSettingsSection, ConsoleApplication1"/>
</configSections>
<serviceSettings>
<setting name="xxx_Service1">
</setting>
<setting name="xxx_Service2">
</setting>
</serviceSettings>
我得到错误:
Unrecognized element 'setting'.
如何解析此配置并获取具有相同属性的 configElement
列表?
最佳答案
这是我从此处改编的代码以适合您的示例。
Code Project: Custom Configuration Sections in app.config or web.config
csc TestCodeApp.cs
.\TestCodeApp.exe
<configuration>
<configSections>
<section name="Services" type="TestCodeApp.ServicesConfig, TestCodeApp"/>
</configSections>
<Services>
<setting name="xxx_Service1" value="this1"/>
<setting name="xxx_Service2" value="this2"/>
</Services>
</configuration>
using System;
using System.Configuration;
using System.Diagnostics;
namespace TestCodeApp {
class TestCode {
static void Main () {
ExeConfigurationFileMap configMap = new ExeConfigurationFileMap { ExeConfigFilename = "TestCodeApp.exe.config" };
Configuration config = ConfigurationManager.OpenMappedExeConfiguration (configMap, ConfigurationUserLevel.None);
var section = (ServicesConfig) config.GetSection ("Services");
// This fixes the below error
// error CS0122: 'System.Configuration.ConfigurationElement.this[System.Configuration.ConfigurationProperty]' is inaccessible due to its protection level
section.SectionInformation.UnprotectSection ();
var services = section.Services;
for (int i = 0; i < services.Count; i++)
{
Console.WriteLine(services[i].Name + " - " + services[i].Value);
}
}
}
public class ServicesConfig : ConfigurationSection {
[ConfigurationProperty("", IsRequired = true, IsDefaultCollection = true)]
[ConfigurationCollection(typeof(ServicesCollection), AddItemName = "setting")]
public ServicesCollection Services {
get { return ((ServicesCollection) (this[""])); }
}
}
[ConfigurationCollection (typeof (ServicesElement))]
public class ServicesCollection : ConfigurationElementCollection {
protected override ConfigurationElement CreateNewElement () {
return new ServicesElement ();
}
protected override object GetElementKey (ConfigurationElement element) {
return ((ServicesElement) (element)).Name;
}
public ServicesElement this [int index] {
get {
return (ServicesElement) BaseGet (index);
}
}
}
public class ServicesElement : ConfigurationElement {
[ConfigurationProperty ("name", DefaultValue = "",
IsKey = true, IsRequired = true)]
public string Name {
get {
return ((string) (base["name"]));
}
set {
base["name"] = value;
}
}
[ConfigurationProperty ("value", DefaultValue = "",
IsKey = false, IsRequired = false)]
public string Value {
get {
return ((string) (base["value"]));
}
set {
base["value"] = value;
}
}
}
}
我测试过它,效果很好。如果您有任何问题,请告诉我。
关于c# - 自定义配置设置,自定义ClientSettingsSection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52624671/
是否可以在 exe 配置中更改 ClientSettingsSection (System.Configuration.ClientSettingsSection) 中的设置值?不幸的是,Client
我是一名优秀的程序员,十分优秀!