using System; using System.Collections.Generic; namespace RuriLib { /// /// Allows to make a switch statement on the Type of the object, and executes a different Action depending on its outcome. /// public class TypeSwitch { Dictionary> matches = new Dictionary>(); /// /// Adds a case to the TypeSwitch. /// /// The type /// The action to perform /// The TypeSwitch itself public TypeSwitch Case(Action action) { matches.Add(typeof(T), (x) => action((T)x)); return this; } /// /// Runs the switch statement on an object and executes the corresponding Action. /// /// The object on which you want to execute the switch statement public void Switch(object x) { matches[x.GetType()](x); } } }