using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var t1 = new Type1();
var t2 = new Type2();
var t3 = new Type3();
var handlers = new Handlers();
HandlersStorage.AddHandler<Type1>(handlers.HaldlerOfType1);
HandlersStorage.AddHandler<Type2>(handlers.HaldlerOfType2);
HandlersStorage.AddHandler<Type3>((object o) => { Console.WriteLine("Haldler of Type3 is lambda"); });
var a1 = HandlersStorage.GetHandler(t1.GetType());
a1(t1);
var a2 = HandlersStorage.GetHandler(t2.GetType());
a2(t2);
var a3 = HandlersStorage.GetHandler(t3.GetType());
a3(t3);
Console.ReadKey();
}
}
public static class HandlersStorage
{
private static Dictionary<Type, Action<object>> _handlers = new Dictionary<Type, Action<object>>();
public static void AddHandler<T>(Action<object> action)
{
_handlers.Add(typeof(T),action);
}
public static Action<object> GetHandler(Type type)
{
return _handlers[type];
}
}
public class Handlers
{
public void HaldlerOfType1(object o)
{
Console.WriteLine("HaldlerOfType1");
}
public void HaldlerOfType2(object o)
{
Console.WriteLine("HaldlerOfType2");
}
}
class Type1
{
}
class Type2
{
}
class Type3
{
}
}