I need a mechanism for unifying constant objects.
Sort of like a generalization of String.Intern.
The constants can be nested (think pure Lisp or
immutable DOM).
I need a kind of weak-reference dictionary, so that
garbage constants will automatically be removed.
At present, the best idea I can come up with is to
use a System.SortedDictionary and populate it
with wrappers based on weak references. The
constant objects will refer back to their wrappers,
and their finalizer will remove them from the
dictionary. The wrappers will be compared by
comparing the wrapped constants.
I'm not too happy about this idea. When a constant
has been created (which will happen very frequently),
a weak reference wrapper will have to be created
before the dictionary lookup - I'd rather postpone
this and only do it when the lookup produces no
match.
Besides, it seems the WeakReference prevents me
from writing verifiable code, though I'm not sure
about this.
Any other ideas?
Is an open-source/shared-source implementation
of SortedDictionary (or similar) available? This
might be a good starting point for me in constructing
a better solution.
Btw, is there a generics version of WeakReference?
Thanks/Ole N.
Guffa - 19 Apr 2006 18:27 GMT
> Btw, is there a generics version of WeakReference?
Not until now:
public class WeakReference<T> : WeakReference {
public WeakReference(T target) : base(target) {}
public WeakReference(T target, bool trackResurrection) : base(target,
trackResurrection) {}
protected WeakReference(System.Runtime.Serialization.SerializationInfo
info, System.Runtime.Serialization.StreamingContext context) : base(info,
context) {}
public new T Target {
get { return (T)base.Target; }
set { base.Target = value; }
}
}