Skip to content

Instantly share code, notes, and snippets.

@ormaaj
Created September 3, 2013 09:23
Show Gist options
  • Save ormaaj/6421574 to your computer and use it in GitHub Desktop.
Save ormaaj/6421574 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
namespace Program {
interface Ifc1 { void Print(string s); }
interface Ifc2 { void Print(string s); }
interface Ifc3 : Ifc1 { void Print(string s); }
class A : Ifc1, Ifc2 {
public void Print(string s) { Console.WriteLine("Called with: {0}", s); }
void Ifc1.Print(string s) { Console.WriteLine("Ifc1: {0}", s); }
void Ifc2.Print(string s) { Console.WriteLine("Ifc2: {0}", s); }
}
class B : A {
public B() {
Print("B");
((Ifc1)this).Print("B");
}
}
class C : B, Ifc3 {
void Ifc3.Print(string s) { Console.WriteLine("Ifc3: {0}", s); }
}
class TestCases {
public static void MultiInterface() {
var o = new C();
Ifc1 i = o as Ifc1; // Ifc1 i = (Ifc1)o
Ifc2 j = o as Ifc2; // Ifc2 j = (Ifc2)o
Ifc3 k = o as Ifc3;
o.Print("object");
if (i != null && j != null && k != null) {
i.Print("iface 1");
j.Print("iface 2");
k.Print("iface 3");
((Ifc3)(A)o).Print("yo");
((A)(Ifc3)o).Print("jo");
}
}
public static void BaseImplementation() {
var o = new B();
o.Print("object");
}
}
class Program {
static void Main(string[] args) {
TestCases.MultiInterface();
// TestCases.BaseImplementation();
Console.Read();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment