I\'ve found the \"ThreadStatic\" attribute to be extremely useful recently, but makes me now want a \"ThreadLocal\" type attribute that lets me hav
I ended up implementing and testing a version of what I had originally suggested:
public class ThreadLocal { [ThreadStatic] private static Dictionary _lookupTable; private Dictionary LookupTable { get { if ( _lookupTable == null) _lookupTable = new Dictionary(); return _lookupTable; } } private object key = new object(); //lazy hash key creation handles replacement private T originalValue; public ThreadLocal( T value ) { originalValue = value; } ~ThreadLocal() { LookupTable.Remove(key); } public void Set( T value) { LookupTable[key] = value; } public T Get() { T returnValue = default(T); if (!LookupTable.TryGetValue(key, out returnValue)) Set(originalValue); return returnValue; } }