如何使用ConfigurationCollection中的键(数组键访问)检索ConfigurationElement?

时间:2013-10-11 02:29:45

标签: c# asp.net arrays configuration indexing

我需要在我的自定义配置部分执行类似的操作:

ConfigurationManager.ConnectionStrings["mongodb"]

上面的字符串“mongodb”是我用来访问System.Configuration.ConnectionStringSettings类型的de元素的键。我希望对我的自定义集合做同样的事情:

[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);
        }
    }

}

我想知道上面评论的方法是获得我想要的方式......但我不知道如何....我想要访问的密钥类型是整数。

假设我有以下配置:

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

如何使用... Section.Questions [3]访问第一个元素(id = 3)(3不是位置,而是键)?

4 个答案:

答案 0 :(得分:4)

感谢Aleksei Chepovoi的消遣。解决方案如下:

[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);
        }
    }

}

答案 1 :(得分:3)

您可以强制此重载通过其键检索configurationElement项:

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

答案 2 :(得分:1)

我假设您的自定义配置部分的名称为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);
}

答案 3 :(得分:0)

[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);
    }

}