Skip to content

Instantly share code, notes, and snippets.

@hodzanassredin
Created October 27, 2011 20:29
Show Gist options
  • Select an option

  • Save hodzanassredin/1320778 to your computer and use it in GitHub Desktop.

Select an option

Save hodzanassredin/1320778 to your computer and use it in GitHub Desktop.
twin objects pattern
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Twins
{
class Program
{
static void Main(string[] args)
{
var c = new C();
c.Foo();
((B)c).Bar();
c.WhatEver();
B b = c;
b.Bar();
C c2 = b;
c2.WhatEver();
Console.WriteLine(Object.ReferenceEquals(c, c2));
Console.ReadKey();
}
}
public class A
{
public void Foo() {
Console.WriteLine("A.Foo");
}
}
public class B
{
public void Bar() {
Console.WriteLine("B.Bar");
}
}
public class C : A//,B -imitatin thought CB
{
public void WhatEver()
{
Console.WriteLine("C.WhatEver");
}
private class CB : B
{
public readonly C c;
public CB(C c)
{
this.c = c;
}
}
public C()
{
b = new CB(this);
}
private readonly CB b;
public static implicit operator B(C obj)
{
return obj.b;
}
public static implicit operator C(B obj)
{
if (obj is CB) return (obj as CB).c;
throw new ArgumentException();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment