Guest User

Untitled

a guest
Oct 18th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.69 KB | None | 0 0
  1.     public class EntityExtensionDictionary<TKey,TValue> : Dictionary<TKey,TValue>
  2.     {
  3.         private Func<TKey, TValue> _ObjectCreator;
  4.  
  5.         public EntityExtensionDictionary(Func<TKey, TValue> objectCreator)
  6.         {
  7.             _ObjectCreator = objectCreator;
  8.         }
  9.         public EntityExtensionDictionary(IDictionary<TKey, TValue> data, Func<TKey, TValue> objectCreator)
  10.             : this(objectCreator)
  11.         {
  12.             foreach (var kvp in data)
  13.                 this.Add(kvp.Key, kvp.Value);
  14.         }
  15.  
  16.         public new TValue this[TKey key]
  17.         {
  18.             get
  19.             {
  20.                 if (base.Keys.Contains(key))
  21.                     return base[key];
  22.                 var newValue = _ObjectCreator(key);
  23.                 base.Add(key, newValue);
  24.                 return newValue;
  25.             }
  26.             set { base[key] = value; }
  27.         }
  28.     }
Add Comment
Please, Sign In to add comment