0

configurationElementCollection の要素内で nameValueCollection を使用するにはどうすればよいですか?
このようなもの:

  <connectionsSection>
    <vendors>
      <add name="nhonho" userName="name" password="password">
        <add key="someKey" value="someValue" />        
      </add>
    </vendors>
  </connectionsSection>

構成要素「vendors」を作成するところまで行きました。

public class Connections : ConfigurationSection
{
    private static string SectionName = "connectionsSection";

    private static Connections _section;

    public static Connections Section
    {
        get
        {
            return _section ??
                (_section = (Connections)ConfigurationManager.GetSection(SectionName));
        }
    }

    [ConfigurationProperty("vendors")]
    public VendorCollection Vendors
    {
        get
        {
            return base["vendors"] as VendorCollection;
        }
    }
}


public class VendorCollection : ConfigurationElementCollection
{
    private IList<VendorElement> _vendors = new List<VendorElement>();

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

    public VendorElement Get(string proName)
    {
        return BaseGet(proName) as VendorElement;
    }

    public void Add(VendorElement element)
    {
        BaseAdd(element);
    }

    public void Clear()
    {
        BaseClear();
    }
    public void Remove(VendorElement element)
    {
        BaseRemove(element.Name);
    }

    public void Remove(string name)
    {
        BaseRemove(name);
    }

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

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

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((VendorElement)element).Name;
    }
}

public class VendorElement : ConfigurationElement
{
    [ConfigurationProperty("name")]
    public string Name { get { return base["name"] as String; } }

    [ConfigurationProperty("userName")]
    public string UserName { get { return base["userName"] as String; } }

    [ConfigurationProperty("password")]
    public string Password { get { return base["password"] as String; } }

    [ConfigurationProperty("", IsDefaultCollection= true)]
    public NameValueCollection Extensions
    {
        get { return base[""] as NameValueCollection; }
    }
}
4

1 に答える 1