Created
October 2, 2012 09:04
-
-
Save clemensv/3817582 to your computer and use it in GitHub Desktop.
C# class extensibility pattern
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
public interface ISomeExtension | |
{ | |
object Private { get; set; } | |
} | |
public static class SomeExtension | |
{ | |
class State | |
{ | |
public string data; | |
} | |
public static void DoThis(this ISomeExtension that, string message) | |
{ | |
that.Private = new State { data = message }; | |
} | |
public static string DoThat(this ISomeExtension that, string message) | |
{ | |
var state = that.Private as State; | |
if ( state != null ) | |
{ | |
return state.data + message; | |
} | |
else | |
{ | |
return message; | |
} | |
} | |
} | |
class MyClass : ISomeExtension | |
{ | |
object ISomeExtension.Private { get; set; } | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
MyClass mc = new MyClass(); | |
mc.DoThis("Data"); | |
Console.WriteLine(mc.DoThat("AndData")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment