Skip to content

Instantly share code, notes, and snippets.

@idavis
Forked from anonymous/gist:3579198
Created September 1, 2012 16:32
Show Gist options
  • Save idavis/3579605 to your computer and use it in GitHub Desktop.
Save idavis/3579605 to your computer and use it in GitHub Desktop.
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
}
}
}
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