0

我有一个从配置文件中读取的配置部分。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); }
    }

}

我相信这是愚蠢的:)提前谢谢!

4

1 回答 1

0

Kind of an educated guess, but it seems like the method:

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

is the most likely culprit. Specifically, according to this page:

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

Gets the element key for a specified configuration element when overridden in a derived class.

In other words, that method returns the means by which .NET can identify the tag in a config file in a subfolder which can override the parent's tag. It seems reasonable, in that regard, to restrict keys to be unique, so by specifying the ((GroupByElement)(element)).Name property as the key, you are saying it must be unique.

One way around this is probably to return the Name and Product, another would be to return the Name and index within the collection, if that's possible. Yet another way, if you did not care about override behaviors, is to just return a unique Guid every time.

于 2014-09-05T14:42:09.550 回答