【问题标题】:c# ConfigurationSection not returning duplicate namesc# ConfigurationSection 不返回重复名称
【发布时间】:2014-09-05 13:23:42
【问题描述】:

我有一个从配置文件中读取的配置部分。 xml 看起来像这样

<GroupBySection>
    <Groups>
        <Group name="Source" product="Product One">
            <Items>
                <Item name="2003" type="radio" />
                <Item name="2007" type="radio" />
                <Item name="2010" type="radio" />
                <Item name="2013" type="radio" />
                <Item name="o365" type="radio" />
            </Items>
        </Group>
        <Group name="Target" product="Product One">
            <Items>
                <Item name="2003" type="radio" />
                <Item name="2007" type="radio" />
                <Item name="2010" type="radio" />
                <Item name="2013" type="radio" />
                <Item name="o365" type="radio" />
            </Items>
        </Group>
        <Group name="Source" product="Product Two">
            <Items>
                <Item name="2003" type="radio" />
                <Item name="2007" type="radio" />
                <Item name="2010" type="radio" />
                <Item name="2013" type="radio" />
                <Item name="o365" type="radio" />
            </Items>
        </Group>
    </Groups>
</GroupBySection>

当我调用此配置部分并进行计数时,我只看到第一个 Product One 结果。产品二不显示,这是因为名称也是“来源”。我希望它显示所有它们,无论名称是否相同。所以简而言之,即使我想要它,它也不会返回它已经遇到过的同名的任何东西。谁能指出我做错了什么?

代码如下

配置部分

public class GroupByConfiguration : ConfigurationSection
{
    [ConfigurationProperty("Groups")]
    public GroupByElementCollection Groups
    {
        get { return ((GroupByElementCollection)(base["Groups"])); }
        set { base["Groups"] = value; }
    }
}

元素部分

public class GroupByElement : ConfigurationElement
{
    // Group Attributes
    [ConfigurationProperty("name", IsRequired = true)]
    public string Name
    {
        get { return (string)base["name"]; }
    }

    [ConfigurationProperty("product", IsRequired = true)]
    public string Product
    {
        get { return (string)base["product"]; }
    }

    [ConfigurationProperty("Items")]
    public ItemElementCollection Items
    {
        get { return ((ItemElementCollection)(base["Items"])); }
        set { base["Items"] = value; }
    }
}

元素集合

[ConfigurationCollection(typeof(GroupByElement))]
public class GroupByElementCollection : ConfigurationElementCollection
{
    internal const string PropertyName = "Group";

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

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

    protected override bool IsElementName(string elementName)
    {
        return elementName.Equals(PropertyName, StringComparison.InvariantCultureIgnoreCase);
    }

    public override bool IsReadOnly()
    {
        return false;
    }

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

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

    public GroupByElement this[int idx]
    {
        get { return (GroupByElement)BaseGet(idx); }
    }

}

我相信这是愚蠢的 :) 在此先感谢!

【问题讨论】:

    标签: c# configurationsection no-duplicates


    【解决方案1】:

    一种有根据的猜测,但它似乎是方法:

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

    是最有可能的罪魁祸首。具体来说,根据这个页面:

    http://msdn.microsoft.com/en-us/library/system.configuration.configurationelementcollection.getelementkey(v=vs.110).aspx

    在派生类中重写时获取指定配置元素的元素键。

    换句话说,该方法返回 .NET 可以识别子文件夹中配置文件中的标记的方法,该子文件夹可以覆盖父标记。在这方面,将键限制为唯一似乎是合理的,因此通过将 ((GroupByElement)(element)).Name 属性指定为键,就是说它必须是唯一的。

    解决此问题的一种方法可能是返回NameProduct,如果可能的话,另一种方法是返回Name 和集合中的索引。如果您不关心覆盖行为,另一种方法是每次只返回一个唯一的 Guid

    【讨论】:

    • 啊,这就是我的菜鸟程序员,我从来没有发现我正在这样做(使用教程)。我不太担心覆盖行为,因此返回 GUID 可能没问题。我刚刚测试了它,它按预期工作!非常感谢!
    • 随机猜测。此外,当我阅读有关这些类的文档时,我认为它提到了 ConfigurationElementCollectionType.BasicMapAlternate 枚举值也与覆盖行为有关。作为额外的预防措施,您可能需要考虑将其更改为不可覆盖的。祝你好运!
    • 会的!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2021-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多