How to get ConfigurationElement using key (access to array key) in ConfigurationCollection?

I need to do something like this in my user settings section:

ConfigurationManager.ConnectionStrings["mongodb"]

The "mongodb" line above is the key that I use to access an element of type System.Configuration.ConnectionStringSettings. I want to do the same with my collection:

[ConfigurationCollection(typeof(Question))]
public class QuestionCollection : ConfigurationElementCollection
{   

    public override bool IsReadOnly()
    {
        return false;
    }

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

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((Question)element).id;
    }

    //Is here?
    public Question this[int idx]
    {
        get {
            return (Question)BaseGet(idx);
        }

        set
        {
            if (BaseGet(idx) != null)
                BaseRemoveAt(idx);

            BaseAdd(idx, value);
        }
    }

}

I was interested that the method described above is a way to get what I want ... But I do not know how .... The type of key that I want to use for access is an integer.

Suppose I have the following configuration:

    <securityQuestions>
    <questions>
      <add id="3" value="What is your name?" default="true"/>
      <add id="4" value="What is your age?"/>
    </questions>
</securityQuestions>

How can I access the first element (id = 3) using ... Section.Questions [3] (3 is not a position, but a key)?

+4
4

, SecurityQuestionsSection.

, :

public class SecurityQuestionsSection: ConfigurationSection
{
    [ConfigurationProperty("questions", IsRequired = true, IsDefaultCollection = true)]
    public QuestionCollection Questions
    {
        get
        {
            return (QuestionCollection)base["questions"];
        }
    }
}

, - :

var customConfigSection = (SecurityQuestionsSection)ConfigurationManager
                                        .GetSection("securityQuestionsSection");

var firstElementId = customConfigSection.Questions[0].Id;  

, !

: . .

1) Linq :

var elementWithIdOfThree = customConfigSection.Questions
                                   .FirstOrDefault(item => item.Id == 3);  

2) QuestionCollection :

public Question GetQuestionWithId(int id)
{
    return this.FirstOrDefault(item => item.Id == id);
}
+1

. :

[ConfigurationCollection(typeof(Question))]
public class QuestionCollection : ConfigurationElementCollection
{   

    public override bool IsReadOnly()
    {
        return false;
    }

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

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((Question)element).id;
    }

    public Question this[int id]
    {
        get
        {
           return this.OfType<Question>().FirstOrDefault(item => item.id == id);
        }
    }

}
+4

You can force this overload to get the element configurationElementwith your key:

public Question GetQuestion(int id)
{
    get
    {
       return (Question)this.BaseGet((object)id);
    }
}
+3
source
[ConfigurationCollection(typeof(Question))]
public class QuestionCollection : ConfigurationElementCollection
{   

    public override bool IsReadOnly()
    {
        return false;
    }

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

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((Question)element).id;
    }

    public Question GetQuestion(int id)
    {
        return (Question)this.BaseGet(id);
    }

}
0
source

All Articles