6

Concider the following config section:

<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>

I've implemented the parsing of this as a c# IConfigSectionHandler. Now I understand that this method is deprecated, and that I should use ConfigurationSection, ConfigurationElements and ConfigurationElementCollections. I have no problem understanding the examples of this on the web (msdn and SO). But all the examples I've seen so far have used one of the properties as a key. My elements are unique as a combination of the Plugin name, MessageType and MessageSubType. The MessageSubType is also optional. Could i parse a config section that looks like this using the recommended classes, or do I have to alter my configuration to fit the regime of the ConfigurationClasses by e.g. adding a "dummy" key?

4

2 回答 2

5

不。

但是要避免使用密钥,您需要做更多的工作。

具体类型KeyValueConfigurationCollection允许通过设置一些属性轻松创建配置集合。

要创建更自定义的集合,需要扩展摘要ConfigurationElementCollection(但这仍将基于 . 使用的添加/删除/清除模型,<appSettings>但允许配置元素名称,但这仍然基于为每个集合的成员(这由覆盖确定,GetElementKey因此不需要直接包含在 XML 中)。

或者,您可以通过扩展创建您自己的、完全自定义的配置集合ConfigurationElement,但您需要自己完成解析子元素的所有工作(记住ConfigurationElementCollection它本身就是 的子类ConfigurationElement)。

于 2013-06-26T07:07:28.357 回答
1

所以基于理查德的优秀回答,我决定重写我的配置解析。结果如下:

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; }
        }
    }
}
于 2013-06-26T12:55:09.777 回答