Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class EntityExtensionDictionary<TKey,TValue> : Dictionary<TKey,TValue>
- {
- private Func<TKey, TValue> _ObjectCreator;
- public EntityExtensionDictionary(Func<TKey, TValue> objectCreator)
- {
- _ObjectCreator = objectCreator;
- }
- public EntityExtensionDictionary(IDictionary<TKey, TValue> data, Func<TKey, TValue> objectCreator)
- : this(objectCreator)
- {
- foreach (var kvp in data)
- this.Add(kvp.Key, kvp.Value);
- }
- public new TValue this[TKey key]
- {
- get
- {
- if (base.Keys.Contains(key))
- return base[key];
- var newValue = _ObjectCreator(key);
- base.Add(key, newValue);
- return newValue;
- }
- set { base[key] = value; }
- }
- }
Add Comment
Please, Sign In to add comment