次のクラス階層があります。
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
派生クラスでオブジェクト初期化子を使用して型を許可するようにするにはどうすればよいMyEnum
ですか?Add
のメソッドが表示されるはずSpecificRow
です。
更新: 追加のインターフェースを実装したSpecificRow
ので、次のようになります。
public class SpecificRow : Row, IXmlSerializable,
System.Collections.IEnumerable,
ICollection<KeyValuePair<MyEnum, string>>
{ }
ただし、それでも同じAdd
エラーが発生します。次に実装してみますIDictionary<MyEnum, string>
。