Overriding explicit implementations of an interface?

What is the correct way to override explicit interface implementations in a child class?

public interface ITest
{
    string Speak();
}

public class ParentTest : ITest
{
    string ITest.Speak()
    {
        return "Meow";
    }
}

public class ChildTest : ParentTest
{
    // causes compile time errors
    override string ITest.Speak()
    {
        // Note: I'd also like to be able to call the base implementation
        return "Mooo" + base.Speak();
    }
}

The above is the best prerequisite for the syntax, but obviously this is wrong. It causes the following compile time errors:

error CS0621:

`ChildTest.ITest.Speak () ': virtual or abstract elements cannot be private

error CS0540:

ChildTest.ITest.Speak()': containing type does not implement interface ITest

error CS0106:

The override modifier is not valid for this element.

I really can make this work without using explicit interfaces, so it doesn’t actually block me, but I would really like to know, for my own curiosity , what is the correct syntax if you want to do this with explicit interfaces?

+4
5

. . 13.4.1 # ( , , , # 6.0). :

, , , .

, .

, :

class Base : IBla
{
    void IBla.DoSomething()
    {
        this.DoSomethingForIBla();
    }

    protected virtual void DoSomethingForIBla()
    {
        ...
    }
}

class Derived : Base
{
    protected override void DoSomethingForIBla()
    {
        ...
    }
}
+5

protected virtual , , :

public class ParentTest : ITest
{
    protected virtual string Speak_Impl()
    {
        return "Meow";
    }
    string ITest.Speak()
    {
        return Speak_Impl();
    }
}

public class ChildTest : ParentTest
{
    protected override string Speak_Impl()
    {
        return "Mooo";
    }
}
+1

, , , , " ".

, , , . .

public class ChildTest : ParentTest, ITest
{
  string ITest.Speak()
  {
    return "Mooo";
  }
  // Note: any other interface functions will call ParentTest implementation
}

"" ParentTest ITest.Speak ChildTest, , ChildTest.

, . , , , ...

, .

public class ChildTest : ParentTest, ITest
{
  string ITest.Speak()
  {
    return "Mooo" + typeof(ParentTest).GetMethod("ITest.Speak", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(this, new object[0]) as string;
  }
}

NB, , . "MyNamespace.ITest.Speak"

/ , / , :

public class ChildTest : ParentTest, ITest
{
  static ChildTest()
  {
    baseSpeakMethodInfo = typeof(ParentTest).GetMethod("ITest.Speak", BindingFlags.Instance | BindingFlags.NonPublic);
  }

  static private MethodInfo baseSpeakMethodInfo;

  public ChildTest()
  {
    baseSpeak = baseSpeakMethodInfo.CreateDelegate(typeof(Func<string>), this) as Func<string>;
  }

  private Func<string> baseSpeak;

  string ITest.Speak()
  {
    return "Mooo" + baseSpeak();
  }
}

, , , , . , ( ) , .

+1

public virtual string Speak() public override string Speak() . . , , .

0

You cannot override explicit implementations of an interface. They cannot be virtual, so there is no way to directly override them. However, you can make them indirectly virtual by calling them a protected virtual member:

public interface ITest
{
    string Speak();
}

public class ParentTest : ITest
{
    string ITest.Speak()
    {
        return Speak();
    }

    protected virtual string Speak()
    {
        return "Meow";
    }

}

public class ChildTest : ParentTest
{
    protected override string Speak()
    {
        return "Mooo";
    }
}
0
source

All Articles