C # object initializer that wants to use the wrong Add method

I have the following class hierarchy:

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) { }
}

However, trying to do the following gives an error:

var result = new SpecificRow
    {
        {MyEnum.Value, ""},
        {MyEnum.OtherValue, ""}
    };

I get this error:

The best overloaded Add method "Row.Add (string, string)" for the collection initializer has several invalid arguments

How can I make sure that using an object initializer in a derived class SpecificRowaccepts a type MyEnum? It looks like he should see method Addc SpecificRow.

Update: I implemented an additional interface on SpecificRow, so now it looks like this:

public class SpecificRow : Row, IXmlSerializable,
    System.Collections.IEnumerable,
    ICollection<KeyValuePair<MyEnum, string>>
{ }

However, I still get the same error Add. I will try to execute IDictionary<MyEnum, string>next.

+5
3

ICollection.Add(x). ,

new SpecificRow {
    { ? }
}

# Add Add (?); ? , # Add . KeyValuePair <, > . { string, string } , Add(string, string) , Add(KeyValuePair<string, string>).

,

new SpecificRow {
    { MyEnum.Value, "" }
};

void Add(MyEnum key, string value)

.

+7

, IDictionary<string, string> . Add(KeyValuePair<MyEnum, string>) - , SpecificRow, , -, Add, .

, :

  • IDictionary<MyEnum, string> IDictionary<MyEnum, string>, (ICollection<KeyValuePair<MyEnum, string>> ..).
  • IDictionary<MyEnum, string> IDictionary<MyEnum, string>, .
  • Row Row<T> IDictionary<T, string>, . SpecificRow Row<MyEnum> Row.
+5

Ruben , Add(MyEnum key, string value), :

var result = new SpecificRow
{
    new KeyValuePair<MyEnum, string>(MyEnum.Value, ""}),
    new KeyValuePair<MyEnum, string>(MyEnum.OtherValue, ""})
};
+1

All Articles