C# 在配置节中创建动态键值对
我正在编写一个类来描述配置部分,我正在寻找一种可能的方法来满足以下场景:C# 在配置节中创建动态键值对,c#,asp.net,configuration,C#,Asp.net,Configuration,我正在编写一个类来描述配置部分,我正在寻找一种可能的方法来满足以下场景: <plugins> <add name="resize" maxheight="500px" maxwidth="500px"/> <add name="watermark" font="arial"/> </plugins> 其中列表中的每个项都可以包含不同的属性以及所需的name属性。设置默认部分非常简单,但我现在被困在如何添加动态键/值对的问题上。
<plugins>
<add name="resize" maxheight="500px" maxwidth="500px"/>
<add name="watermark" font="arial"/>
</plugins>
其中列表中的每个项都可以包含不同的属性以及所需的name属性。设置默认部分非常简单,但我现在被困在如何添加动态键/值对的问题上。有什么想法吗
/// <summary>
/// Represents a PluginElementCollection collection configuration element
/// within the configuration.
/// </summary>
public class PluginElementCollection : ConfigurationElementCollection
{
/// <summary>
/// Represents a PluginConfig configuration element within the
/// configuration.
/// </summary>
public class PluginElement : ConfigurationElement
{
/// <summary>
/// Gets or sets the token of the plugin file.
/// </summary>
/// <value>The name of the plugin.</value>
[ConfigurationProperty("name", DefaultValue = "", IsRequired = true)]
public string Name
{
get { return (string)this["name"]; }
set { this["name"] = value; }
}
// TODO: What goes here to create a series of dynamic
// key/value pairs.
}
/// <summary>
/// Creates a new PluginConfig configuration element.
/// </summary>
/// <returns>
/// A new PluginConfig configuration element.
/// </returns>
protected override ConfigurationElement CreateNewElement()
{
return new PluginElement();
}
/// <summary>
/// Gets the element key for a specified PluginElement
/// configuration element.
/// </summary>
/// <param name="element">
/// The <see cref="T:System.Configuration.ConfigurationElement"/>
/// to return the key for.
/// </param>
/// <returns>
/// The element key for a specified PluginElement configuration element.
/// </returns>
protected override object GetElementKey(ConfigurationElement element)
{
return ((PluginElement)element).Name;
}
}
//
///表示PluginElementCollection集合配置元素
///在配置中。
///
公共类PluginElementCollection:ConfigurationElementCollection
{
///
///表示中的PluginConfig配置元素
///配置。
///
公共类插件元素:ConfigurationElement
{
///
///获取或设置插件文件的标记。
///
///插件的名称。
[ConfigurationProperty(“名称”,DefaultValue=“”,IsRequired=true)]
公共字符串名
{
获取{return(string)this[“name”];}
设置{this[“name”]=value;}
}
//TODO:这里是用来创建一系列动态
//键/值对。
}
///
///创建新的PluginFig配置元素。
///
///
///一个新的PluginFig配置元素。
///
受保护的覆盖ConfigurationElement CreateNewElement()
{
返回新的PluginElement();
}
///
///获取指定PluginElement的元素键
///配置元素。
///
///
///
///返回的键。
///
///
///指定的PluginElement配置元素的元素键。
///
受保护的覆盖对象GetElementKey(ConfigurationElement元素)
{
返回((PluginElement)元素).Name;
}
}
在ConfigurationElement
中,您可以覆盖OnDeserializeUnrecognizedAttribute()
,然后将额外的属性存储在某个地方,例如在字典中:
public class PluginElement : ConfigurationElement
{
public IDictionary<string, string> Attributes { get; private set; }
public PluginElement ()
{
Attributes = new Dictionary<string, string>();
}
protected override bool OnDeserializeUnrecognizedAttribute(string name, string value)
{
Attributes.Add(name, value);
return true;
}
}
公共类PluginElement:ConfigurationElement
{
公共IDictionary属性{get;private set;}
公共插件()
{
属性=新字典();
}
受保护的重写bool OnDeserializedAttribute(字符串名称、字符串值)
{
属性。添加(名称、值);
返回true;
}
}
从
OnDeserializeUnrecognizedAttribute
返回true
表示您已经处理了未解析的属性,并防止ConfigurationElement
基类引发异常,这通常是在您未声明[ConfigurationProperty]时发生的
用于配置xml中的每个属性 我已经回答了这个问题。请注意,您可能需要将SystemConfiguration替换为System.Configuration,因为我使用了和-别名来解决命名冲突。@Morten Mertner-查看您的代码,它似乎只处理了一种默认情况,即您事先知道键/值。我希望能够为每个实例添加自定义对。不,代码用于包含元素列表的完全自定义配置部分。显然,您可能需要修改一些位,因为您希望存储不同的值,但所需的类都在那里。是的,在UserElement
中,这正是我要问的问题。我需要添加一些东西到PluginElement
。您只需将所需的属性添加到PluginElement类中,并使用[ConfigurationElement(Required=false)]
对它们进行修饰,使它们成为可选的。您不能(afaik)使用内置配置子系统使数据完全动态,因此必须提前在类上指定可用选项。谢谢。我去试试,然后再报告。