using System; using System.Collections.Generic; namespace ScriptCs.Contracts { public class Printers { private readonly IObjectSerializer _serializer; private readonly Dictionary> _dictionary = new Dictionary>(); public Printers(IObjectSerializer serializer) { _serializer = serializer; } public void AddCustomPrinter(Func printer) { _dictionary[typeof(T)] = x => printer((T)x); } private string GetStringFor(Type t, object obj) { Func printer; if (_dictionary.TryGetValue(t, out printer)) { return printer(obj); } return _serializer.Serialize(obj); } public string GetStringFor(object obj) { return GetStringFor(obj.GetType(), obj); } public string GetStringFor(T obj) { return GetStringFor(typeof(T), obj); } } }