2

MSDN 中关于制作自定义配置部分的示例,该部分应按如下方式工作,

class RemoteServiceSection : ConfigurationSection
{
    [ConfigurationProperty("remoteServices", IsDefaultCollection=false)]
    [ConfigurationCollection(typeof(RemoteServiceCollection), AddItemName="addService", ClearItemsName="clearServices",
        RemoveItemName="removeService")]
    public RemoteServiceCollection Services
    {
        get
        {
            return this["remoteServices"] as RemoteServiceCollection; 
        }
    }
}

class RemoteServiceCollection : ConfigurationElementCollection, IList<RemoteServiceElement>
{
    public RemoteServiceCollection()
    {
        RemoteServiceElement element = (RemoteServiceElement)CreateNewElement();
        Add(element); 
    }

    public override ConfigurationElementCollectionType CollectionType
    {
        get
        {
            return ConfigurationElementCollectionType.AddRemoveClearMap;
        }
    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new RemoteServiceElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((RemoteServiceElement)element).Hostname;
    }

    protected override string ElementName
    {
        get
        {
            return "remoteService";
        }
    }

    public new IEnumerator<RemoteServiceElement> GetEnumerator()
    {
        foreach (RemoteServiceElement element in this)
        {
            yield return element; 
        }
    }

    public void Add(RemoteServiceElement element)
    { 
        BaseAdd(element, true); 
    }

    public void Clear()
    {
        BaseClear(); 
    }

    public bool Contains(RemoteServiceElement element)
    {
        return !(BaseIndexOf(element) < 0); 
    }

    public void CopyTo(RemoteServiceElement[] array, int index)
    {
        base.CopyTo(array, index); 
    }

    public bool Remove(RemoteServiceElement element)
    {
        BaseRemove(GetElementKey(element));
        return true; 
    }

    bool ICollection<RemoteServiceElement>.IsReadOnly
    {
        get { return IsReadOnly(); } 
    }

    public int IndexOf(RemoteServiceElement element)
    {
        return BaseIndexOf(element); 
    }

    public void Insert(int index, RemoteServiceElement element)
    {
        BaseAdd(index, element); 
    }

    public void RemoveAt(int index)
    {
        BaseRemoveAt(index); 
    }

    public RemoteServiceElement this[int index]
    {
        get
        {
            return (RemoteServiceElement)BaseGet(index); 
        }
        set
        {
            if (BaseGet(index) != null)
            {
                BaseRemoveAt(index); 
            }
            BaseAdd(index, value); 
        }
    }
}

class RemoteServiceElement : ConfigurationElement
{
    public RemoteServiceElement() { }

    public RemoteServiceElement(string ip, string port)
    {
        this.IpAddress = ip;
        this.Port = port; 
    }

    [ConfigurationProperty("hostname", IsKey = true, IsRequired = true)]
    public string Hostname
    {
        get
        {
            return (string)this["hostname"];
        }
        set
        {
            this["hostname"] = value;
        }
    }
    [ConfigurationProperty("ipAddress", IsRequired = true)]
    public string IpAddress
    {
        get
        {
            return (string)this["ipAddress"];
        }
        set
        {
            this["ipAddress"] = value;
        }
    }
    [ConfigurationProperty("port", IsRequired = true)]
    public string Port
    {
        get
        {
            return (string)this["port"];
        }
        set
        {
            this["port"] = value;
        }
    }
}

}

我收到“无法识别的元素‘addService’的错误消息。我想我完全遵循了 MSDN 文章。它可以在这里找到 - http://msdn.microsoft.com/en-us/library/system.configuration.configurationcollectionattribute.aspx

在此先感谢您的帮助。这就是我在 app.config 中写的内容(当然,括号不会出现在这里?):

 <remoteServices>
   <addService hostname="xxxxxxx" ipAddress="xxx.x.xxx.xx" port="xxxx" >
 </remoteServices>

这是 app.config 所要求的,只是出于隐私目的删除特定名称,它们只是字符串:

<configuration>
<configSections>
  <section name="remoteServices" type="AqEntityTests.RemoteServiceSection, 
     AqEntityTests" allowLocation="true" allowDefinition="Everywhere"/>
  </configSections>
  <remoteServices>
   <addService hostname="xxxxxx.xxxxxxx.com" 
            ipAddress="xxx.x.xxx.xx" 
            port="xx" />
  </remoteServices>
4

2 回答 2

1

对于后代:

您的配置应如下所示:

<configuration>
  <configSections>
    <section name="remoteServices" type="AqEntityTests.RemoteServiceSection, 
        AqEntityTests" allowLocation="true" allowDefinition="Everywhere"/>
  </configSections>

  <remoteServices>
    <remoteServices>
      <addService hostname="xxxxxx.xxxxxxx.com" 
         ipAddress="xxx.x.xxx.xx" 
         port="xx" />
    </remoteServices>
  </remoteServices>
</configuration>

为什么?

您添加到节点:

<configSections> 

自定义部分名为:

name="remoteServices"

带类型

type="AqEntityTests.RemoteServiceSection

然后在代码中,将属性添加到自定义部分:

[ConfigurationProperty("remoteServices", IsDefaultCollection=false)]

这意味着您在节点内部创建了具有相同名称的节点。因此,您收到错误“无法识别的元素'addService'”。只是编译器通知你这样的元素不应该在那个节点中。

快速学习自定义配置的两个链接:
Lazy Coders 的自定义配置部分
如何使用集合创建部分

于 2015-02-25T13:40:29.763 回答
0

正如我在这里提到的,您可能还会考虑使用未命名的默认集合

这允许您以您建议的方式添加项目。

于 2011-05-31T13:22:26.603 回答