Skip to content

Instantly share code, notes, and snippets.

@clemensv
Created October 2, 2012 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clemensv/3817582 to your computer and use it in GitHub Desktop.
Save clemensv/3817582 to your computer and use it in GitHub Desktop.
C# class extensibility pattern
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