3

I'm experimenting with explicit implentations of interfaces. This is to strip the intellisense with methods which are not valid in the current context. Use /practical-applications-of-the-adaptive-interface-pattern-the-fluent-builder-context/ as reference. To prove that they would not be callable, I thought I could use the dynamic keyword, because then at least my code would compile. It does compile, but it does not work as expected. The dynamic variable has access to the class methods, but not the interface methods that are explicit implemented.

public interface IAmInterface
{
    void Explicit();
    void Implicit();
}

public class Implementation : IAmInterface
{
    void IAmInterface.Explicit()
    {
    }

    public void Implicit()
    {
    }
    public static Implementation BeginBuild()
    {
        return new Implementation();
    }
}

And here are the 3 tests to prove my point

[Test]
public void TestWorksAsExpected() //Pass
{
    var o = Implementation.BeginBuild();
    o.Implicit();
}

[Test]
public void TestDoesNotWorkWithExplicitImplementation() //Fails
{
    dynamic o = Implementation.BeginBuild();
    o.Explicit();
}

[Test]
public void ButWorksForImplicitImplementation() //Pass
{
    dynamic o = Implementation.BeginBuild();
    o.Implicit();
}

Would anyone be kind enough to explain the reason for this? One example where I wanted this functionality was to prove that I couldnt add more than two players in a TennisGame.

dynamic o = TennisGame.BeginBuild().With("Player A").Versus("Player B");
o.Versus("Player C"); //Should fail. It does, but for another reason
Rob
  • 4,927
  • 12
  • 49
  • 54
MortenRøgenes
  • 676
  • 4
  • 12
  • You wrote `To prove that they would not be callable...` you have proven that if your test fails. – Rafal Jul 24 '12 at 07:32
  • I dont think I have. Take the tennis example; If I have both Versus on a different line, the first one should be allowed, but when using dynamic, it fails. RuntimeBinderException – MortenRøgenes Jul 24 '12 at 07:43

1 Answers1

7

The dynamic variable has access to the class methods, but not the interface methods that are explicit implemented.

Yes, that is correct. dynamic has access to the regular members that would be accessible (based on context etc, usually means "public"). However, the only way, even in regular C#, to invoke explicit interface implementations, is to cast the object to the interface. This remains the case with dynamic.

Implicit interfact implementations are also part of the regular class API, so they are externally available (against the type) to both regular c# and dynamic.

Basically: no, dynamic can not and will not access explicit interface implementations.

Either cast to the interface, or use reflection from the interface type (not the object type).

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Thanks! I guess reflection is the only viable option, but that again seem like overkill. – MortenRøgenes Jul 24 '12 at 07:59
  • In my case I can't cast it to interface because it is a generic interface and IInterface doesn't work either. I don't want to use reflection either. Any suggestions? – adeel41 Aug 10 '16 at 19:40