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 の結果しか表示されません。製品 2 が表示されないのは、名前も「ソース」であるためです。名前が同じかどうかに関係なく、すべて表示したい。要するに、私が望んでいても、すでに出くわした同じ名前のものは返されません。誰が私が間違ったことを指摘できますか?

以下のコード

構成セクション

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

経験に基づいた推測のようなものですが、方法のようです:

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プロパティをキーとして指定することで、一意でなければならないと言っています。

これを回避する 1 つの方法は、おそらくNameandを返すことです。別の方法は、可能であればコレクション内の and インデックスProductを返すことです。Nameさらに別の方法として、オーバーライドの動作を気にしない場合は、Guid毎回一意のものを返すだけです。

于 2014-09-05T14:42:09.550 に答える