Skip to content

thomas3d/TypeSwitch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 

Repository files navigation

TypeSwitch

I got the idea here:

http://stackoverflow.com/questions/7252186/switch-case-on-type-c-sharp/7301514#7301514

public static void TestTypeSwitch()
{
    var ts = new TypeSwitch()
        .Case((int x) => Console.WriteLine("int"))
        .Case((bool x) => Console.WriteLine("bool"))
        .Case((string x) => Console.WriteLine("string"));

    ts.Switch(42);     
    ts.Switch(false);  
    ts.Switch("hello"); 
}
public class TypeSwitch
{
    Dictionary<Type, Action<object>> matches = new Dictionary<Type, Action<object>>();
    public TypeSwitch Case<T>(Action<T> action) { matches.Add(typeof(T), (x) => action((T)x)); return this; } 
    public void Switch(object x) { matches[x.GetType()](x); }
}

Thanks to Christopher from Autodesk (http://stackoverflow.com/users/184528/cdiggins)

Thanks to Stackoverflow

Thanks to Google

My own style: (se file TypeSwitch.cs)

public class TypeSwitch
{
    Dictionary<Type, Func<object, object>> matches = new Dictionary<Type, Func<object, object>>();
    public TypeSwitch Case<T>(Func<T, T> func) { matches.Add(typeof(T), (x) => func((T)x)); return this; }
    public TypeSwitch Default<T>(Func<T, T> func) { matches.Add(typeof(object), (x) => func((T)x)); return this; }
    public object Switch(object x) { return matches[x.GetType()](x); }
}

About

This example/class show you how to make a switch on Type in C#

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages