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 は次のとおりです。プライバシーを保護する目的で特定の名前を x'ing しています。これらは単なる文字列です。

<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

For future generations:

Your config should look like this:

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

Why?

You add to node:

<configSections> 

custom section named:

name="remoteServices"

with type

type="AqEntityTests.RemoteServiceSection

and then in code, you add property to your custom section:

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

Meaning you created node inside node with both having same name. Because of that you have received error "Unrecognized element 'addService'". Just compiler informing you that such element should not be in that node.

Two links for quick learning of custom configuration:
Custom Configuration Sections for Lazy Coders
How to create sections with collections

于 2015-02-25T13:40:29.763 に答える
0

ここで説明するように、名前のないデフォルトのコレクションを使用することも検討してください。

それはあなたがあなたが提案する方法でアイテムを追加することを可能にします。

于 2011-05-31T13:22:26.603 に答える