-
-
Save idavis/3579605 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace ConsoleApplication1 | |
{ | |
interface ITest | |
{ | |
void Method(); | |
} | |
class Parent : ITest | |
{ | |
void ITest.Method() | |
{ | |
Console.WriteLine("Parent"); | |
} | |
} | |
class Child : Parent, ITest | |
{ | |
void ITest.Method() | |
{ | |
Console.WriteLine("Child"); | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var c = new Child(); | |
Parent p = c; | |
object o = p; | |
((ITest)c).Method(); // Child | |
((ITest)p).Method(); // Child | |
((ITest)o).Method(); // Child | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace ConsoleApplication1 | |
{ | |
interface ITest | |
{ | |
void Method(); | |
} | |
class Parent : ITest | |
{ | |
void ITest.Method() { Console.WriteLine("Called Parent.Method. Current type {0}", GetType());} | |
public void ParentMethodAccessor() | |
{ | |
((ITest)this).Method(); | |
} | |
} | |
class Child : Parent, ITest | |
{ | |
void ITest.Method() { Console.WriteLine("Called Child.Method. Current type {0}", GetType()); } | |
public void ChildMethodAccessor() | |
{ | |
((ITest)this).Method(); | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Parent parent = new Parent(); | |
Child child = new Child(); | |
parent.ParentMethodAccessor(); | |
child.ChildMethodAccessor(); | |
ITest parentTest = parent; | |
ITest childTest = child; | |
parentTest.Method(); | |
childTest.Method(); | |
Parent childAsParent = child; | |
childAsParent.ParentMethodAccessor(); | |
ITest childAsParentAsTest = childAsParent; | |
childAsParentAsTest.Method(); | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment