简体   繁体   中英

Collection with equal elements in .Net configuration section

I am interested, if it is possible to have collection with same elements in .Net configuration. Like this, for example:

                <RetrySettings>
                <RetryTurn PeriodBeforeRetry="0:05:00"/>
                <RetryTurn PeriodBeforeRetry="0:10:00"/>
                <RetryTurn PeriodBeforeRetry="0:30:00"/>
                <RetryTurn PeriodBeforeRetry="1:00:00"/>
                <RetryTurn PeriodBeforeRetry="4:00:00"/>
                <RetryTurn PeriodBeforeRetry="8:00:00"/>
                <RetryTurn PeriodBeforeRetry="8:00:00"/>
                <RetryTurn PeriodBeforeRetry="8:00:00"/>
                <RetryTurn PeriodBeforeRetry="8:00:00"/>
                <RetryTurn PeriodBeforeRetry="8:00:00"/>
                <RetryTurn PeriodBeforeRetry="8:00:00"/>
            </RetrySettings>

without adding annoying id="someUniqueId" attributes to each RetryTurn member?

I don't see how to make this, using custom collection, derived from ConfigurationElementCollection ... Any possible solution for this?

Finally I found the workaround. In RetryTurn class define internal Guid property UniqueId and initialize it with new Guid value in default constructor:

public class RetryTurnElement : ConfigurationElement
{
    public RetryTurnElement()
    {
        UniqueId = Guid.NewGuid();
    }

    internal Guid UniqueId { get; set; }

    ...
}

In RetryTurnCollection class override GetElementKey method like this:

    public class RetryTurnCollection : ConfigurationElementCollection
{
    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((RetryTurnElement)element).UniqueId;
    }
    ...
}

Have you tried

public class RetryTurnCollection : ConfigurationElementCollection
{
    protected override object GetElementKey(ConfigurationElement element)
    {
        return element;
    }
    ...
}

Couldn't you use the PeriodBeforeRetry attribute as your unique identifier? GetElementKey() returns an object , so that shouldn't be a problem.

Unless I've misunderstood the question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM