我有以下类层次结构:
public class Row : ICloneable, IComparable, IEquatable<Row>,
IStringIndexable, IDictionary<string, string>,
ICollection<KeyValuePair<string, string>>,
IEnumerable<KeyValuePair<string, string>>,
System.Collections.IEnumerable
{ }
public class SpecificRow : Row, IXmlSerializable,
System.Collections.IEnumerable
{
public void Add(KeyValuePair<MyEnum, string> item) { }
}
但是,尝试执行以下操作会出错:
var result = new SpecificRow
{
{MyEnum.Value, ""},
{MyEnum.OtherValue, ""}
};
我收到此错误:
集合初始值设定项的最佳重载 Add 方法 'Row.Add(string, string)' 有一些无效参数
我怎样才能使它在派生类上使用对象初始值设定项SpecificRow
允许 type MyEnum
?似乎它应该Add
在SpecificRow
.
更新: 我实现了一个额外的接口,SpecificRow
所以它现在看起来像这样:
public class SpecificRow : Row, IXmlSerializable,
System.Collections.IEnumerable,
ICollection<KeyValuePair<MyEnum, string>>
{ }
但是,我仍然遇到同样的Add
错误。接下来我将尝试实施IDictionary<MyEnum, string>
。