Skip to content

Instantly share code, notes, and snippets.

@lantoli
Created November 18, 2014 23:30
Show Gist options
  • Save lantoli/b7b8b985a5dc86743b6c to your computer and use it in GitHub Desktop.
Save lantoli/b7b8b985a5dc86743b6c to your computer and use it in GitHub Desktop.
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