Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Type matching in c#
public class HandleSomething
{
public void DoStuff(Object instance)
{
TypeMatch.Case(instance,
(GnarlyException e) => { HandleGnarlyException(e); },
() => {
DoTheImportantStuff();
});
}
}
public class TypeMatch
{
public static void Case<T1>(object o, Action<T1> a1, Action _else = null)
{
if (o is T1)
{
a1((T1)o);
return;
}
if (_else != null) _else();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment