我有一個配置節讀取配置文件。該xml看起來像這樣c#ConfigurationSection不返回重複的名字
<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>
當我打電話給這個配置節並做一個計數,我只看到第一個產品一結果。產品二不顯示,這是因爲該名稱也是「源」。無論名稱是否相同,我都希望它顯示所有它們。總而言之,即使我想要它,它也不會返回與它已經遇到的同名的任何東西。任何人都可以指出什麼是錯誤的嗎?
代碼如下
的ConfigurationSection
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); }
}
}
我相信這是愚蠢的東西:)謝謝我前進!
啊,這是我的noob程序員,我從來沒有發現我正在這樣做(使用教程)。我不太擔心重寫行爲,所以返回一個GUID可能沒問題。我剛剛測試過,它按預期工作!非常感謝! – jAC 2014-09-05 14:53:55
隨機猜測ftw。另外,當我閱讀有關這些類的文檔時,我認爲它提到了'ConfigurationElementCollectionType.BasicMapAlternate'枚舉值也與覆蓋行爲有關。您可能需要考慮將其更改爲無法超越的預防措施。祝你好運! – welegan 2014-09-05 15:20:01
會做!非常感謝! – jAC 2014-09-05 17:35:41