Guest User

RenderTools idea

a guest
Jun 21st, 2012
2,157
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public sealed class RenderTools
  2. {
  3.     private readonly IDictionary<Type, Func<object, Freezable>> _toolFactories = new Dictionary<Type, Func<object, Freezable>>();
  4.  
  5.     private readonly IDictionary<object, Freezable> _toolCache = new Dictionary<object, Freezable>();
  6.  
  7.     public void RegisterTool<TKey, TResult>(Func<TKey, TResult> toolFactory)
  8.         where TResult : Freezable
  9.     {
  10.         this._toolFactories[typeof(TKey)] = new Func<object, Freezable>(
  11.             p =>
  12.             {
  13.                 var tool = toolFactory((TKey)p);
  14.                 tool.Freeze();
  15.                 return tool;
  16.             });
  17.     }
  18.  
  19.     public TResult GetTool<TResult>(object toolKey)
  20.         where TResult : Freezable
  21.     {
  22.         Freezable result;
  23.  
  24.         if (!this._toolCache.TryGetValue(toolKey, out result))
  25.         {
  26.             result = this._toolFactories[toolKey.GetType()](toolKey);
  27.             this._toolCache.Add(toolKey, result);
  28.         }
  29.  
  30.         return (TResult)result;
  31.     }
  32. }
RAW Paste Data