C# .net自定义配置节元素必须有键吗?
编辑以下配置部分:C# .net自定义配置节元素必须有键吗?,c#,nested,configurationsection,C#,Nested,Configurationsection,编辑以下配置部分: <PluginSection> <Plugins> <Plugin name="Plug1"> <add MessageType="1" MessageSubType="1" Ringtone="chime.wav" Vibrate="1000:0:1"/> <add MessageType="1" MessageSubType="2" Ringtone="ch
<PluginSection>
<Plugins>
<Plugin name="Plug1">
<add MessageType="1" MessageSubType="1" Ringtone="chime.wav" Vibrate="1000:0:1"/>
<add MessageType="1" MessageSubType="2" Ringtone="chime2.wav" Vibrate="1000:0:1"/>
</Plugin>
<Plugin name="Plug2">
<add MessageType="1" MessageSubType="1" Ringtone="chime.wav"/>
<add MessageType="1" MessageSubType="2" Ringtone="chime2.wav"/>
<add MessageType="2" Ringtone="chime3.wav"/>
</Plugin>
</Plugins>
</PluginSection>
我已经将其解析实现为一个c#IConfigSectionHandler。现在我知道这个方法已经被弃用了,我应该使用ConfigurationSection、ConfigurationElements和ConfigurationElementCollections。我完全理解网络上的例子(msdn等)。但到目前为止,我看到的所有示例都使用其中一个属性作为键。My元素是插件名称、MessageType和MessageSubType的唯一组合。MessageSubType也是可选的。我是否可以使用推荐的类来解析类似于此的配置部分,或者我是否必须通过添加“虚拟”键等方式更改配置以适应配置类的制度?否
但是为了避免钥匙,你需要做更多的工作
具体类型允许通过设置一些属性轻松创建配置集合
要创建更定制的集合,需要扩展摘要(但这仍然基于使用的添加/删除/清除模型,因此不需要直接包含在XML中)
或者,您可以通过扩展来创建自己的、完全自定义的配置集合,但您需要自己完成解析子元素的所有工作(请记住ConfigurationElementCollection
本身就是ConfigurationElement
的子类)。因此,根据Richard的出色回答,我决定重写我的配置解析。结果如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
namespace PluginConfiguration
{
public class PluginConfigSection : ConfigurationSection
{
[ConfigurationProperty("Plugins", IsDefaultCollection = true)]
[ConfigurationCollection(typeof(PluginCollection), AddItemName = "Plugin")]
public PluginCollection Plugins
{
get
{
PluginCollection coll = (PluginCollection)base["Plugins"];
return coll;
}
}
}
public class PluginCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new MessageMappingElementCollection();
}
protected override object GetElementKey(ConfigurationElement element)
{
MessageMappingElementCollection coll = element as MessageMappingElementCollection;
return coll.Name;
}
}
public class MessageMappingElementCollection : ConfigurationElementCollection
{
[ConfigurationProperty("name", IsRequired = true, IsKey = true)]
public string Name
{
get { return this["name"].ToString(); }
set { this["name"] = value; }
}
protected override ConfigurationElement CreateNewElement()
{
return new MessageMappingElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
MessageMappingElement msgElement = element as MessageMappingElement;
string ret = String.Format("{0}|{1}", msgElement.MessageType, msgElement.MessageSubType);
return ret;
}
}
public sealed class MessageMappingElement : ConfigurationElement
{
public MessageMappingElement()
{
MessageType = 0;
MessageSubType = 0;
RingTone = "";
Description = "";
Vibrate = "";
}
[ConfigurationProperty("MessageType", IsRequired = true)]
public int MessageType
{
get { return int.Parse(this["MessageType"].ToString()); }
set { this["MessageType"] = value; }
}
[ConfigurationProperty("MessageSubType", IsRequired = false)]
public int MessageSubType
{
get { return int.Parse(this["MessageSubType"].ToString()); }
set { this["MessageSubType"] = value; }
}
[ConfigurationProperty("RingTone", IsRequired = false)]
public string RingTone
{
get { return this["RingTone"].ToString(); }
set { this["RingTone"] = value; }
}
[ConfigurationProperty("Description", IsRequired = false)]
public string Description
{
get { return this["Description"].ToString(); }
set { this["Description"] = value; }
}
[ConfigurationProperty("Vibrate", IsRequired = false)]
public string Vibrate
{
get { return this["Vibrate"].ToString(); }
set { this["Vibrate"] = value; }
}
}
}
好啊谢谢因此,重写GetElementKey,返回字符串“pluginName+MessageType.ToString()+MessageSubType.ToString()(可能有一些管道分隔)应该可以解决这个问题?或者可能只是一个运行中的数字?@janpeterjetmundsen:在对GetElementKey的多次调用中,每个集合元素的结果需要一致(所以一个连续的数字,除非记住,否则是不起作用的)。