Custom config section containing collection

佐手、 提交于 2019-11-30 11:51:07

In your definition of FileInfoCollection in the method CreateNewElement you create FileInfoCollection which is wrong. Overridden CreateNewElement should return new collection element, not the new collection:

public class FileInfoCollection : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new FileInfoElement();
    }

    protected override object GetElementKey (ConfigurationElement element)
    {
        return ((FileInfoElement)element).Extension;
    }
}

Regarding your desired configuration, probably the simplest implementation will look like:

public class AuthorisedClientsSection : ConfigurationSection {
    [ConfigurationProperty("", IsDefaultCollection = true)]
    public AuthorisedClientElementCollection Elements {
        get { return (AuthorisedClientElementCollection)base[""];}
    }
}

public class AuthorisedClientElementCollection : ConfigurationElementCollection {
    const string ELEMENT_NAME = "AuthorisedClient";

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

    protected override string ElementName {
        get { return ELEMENT_NAME; }
    }

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

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

public class AuthorisedClientElement : ConfigurationElement {
    const string NAME = "name";

    [ConfigurationProperty(NAME, IsRequired = true)]
    public string Name {
        get { return (string)base[NAME]; }
    }

    [ConfigurationProperty("", IsDefaultCollection = true)]
    public QueueElementCollection Elements {
        get { return (QueueElementCollection)base[""]; }
    }
}

public class QueueElementCollection : ConfigurationElementCollection {
    const string ELEMENT_NAME = "Queue";

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

    protected override string ElementName {
        get { return ELEMENT_NAME; }
    }

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

    protected override object GetElementKey(ConfigurationElement element) {
        return ((QueueElement)element).Id;
    }
}

public class QueueElement : ConfigurationElement {
    const string ID = "id";

    [ConfigurationProperty(ID, IsRequired = true)]
    public int Id {
        get { return (int)base[ID]; }
    }
}

And the test:

var authorisedClientsSection = ConfigurationManager.GetSection("AuthorisedClients")
    as AuthorisedClientsSection;

foreach (AuthorisedClientElement client in authorisedClientsSection.Elements) {
    Console.WriteLine("Client: {0}", client.Name);

    foreach (QueueElement queue in client.Elements) {
        Console.WriteLine("\tQueue: {0}", queue.Id);
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!