Created
November 18, 2014 23:30
-
-
Save lantoli/b7b8b985a5dc86743b6c to your computer and use it in GitHub Desktop.
Reto 6 MSDN http://blogs.msdn.com/b/esmsdn/archive/2014/11/14/retosmsdn-reto-6-cach-233-de-objetos-en-c.aspx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
namespace Reto_6 | |
{ | |
[DebuggerDisplay("Count = {Count}, ActiveCount = {ActiveCount}")] | |
[DebuggerTypeProxy(typeof(CacheDebugView))] | |
public class Cache | |
{ | |
[DebuggerBrowsable(DebuggerBrowsableState.Never)] | |
private readonly Dictionary<object, WeakReference> dict = new Dictionary<object, WeakReference>(); | |
public void Add(object key, object value) { | |
dict.Add(key, new WeakReference(value)); | |
} | |
[DebuggerBrowsable(DebuggerBrowsableState.Never)] | |
public int Count { | |
get { return dict.Count; } | |
} | |
[DebuggerBrowsable(DebuggerBrowsableState.Never)] | |
public int ActiveCount { | |
get { return dict.Values.Count(x => x.Target != null); } | |
} | |
public object this[object key] { | |
get { return dict[key].Target; } | |
} | |
[DebuggerDisplay("Key = {Key}, Value = {Value}")] | |
internal class Pair | |
{ | |
[DebuggerBrowsable(DebuggerBrowsableState.Never)] | |
public object Key { get; set; } | |
[DebuggerBrowsable(DebuggerBrowsableState.Never)] | |
public object Value { get; set; } | |
} | |
internal class CacheDebugView | |
{ | |
[DebuggerBrowsable(DebuggerBrowsableState.Never)] | |
private readonly Cache cache; | |
public CacheDebugView(Cache cache) { | |
this.cache = cache; | |
} | |
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] | |
public Pair[] Pairs { | |
get { | |
return cache.dict.Select(x => new Pair {Key = x.Key, Value = x.Value.Target}).ToArray(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment