A collection with equal elements in the .Net configuration section

I am wondering if it is possible to have a collection with the same elements in a .Net configuration. For example, 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 attributes id="someUniqueId"to each member RetryTurn?

I don’t see how to do this using a custom collection derived from ConfigurationElementCollection... Any possible solution for this?

+3
source share
3 answers

Finally, I found a workaround. In the class, RetryTurndefine an internal property Guid UniqueIdand initialize it with the new value Guidin the default constructor:

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

    internal Guid UniqueId { get; set; }

    ...
}

RetryTurnCollection GetElementKey :

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

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

Unable to use attribute PeriodBeforeRetryas unique identifier? GetElementKey()returns object, so this should not be a problem.

If I do not understand this question.

0
source

Source: https://habr.com/ru/post/1715416/


All Articles