Thanks for the quick reply Barry.

To what purpose do you keep a mapping of strings to components? I think
knowing the answer to this question would help people solve your
problem.

I actually simplified the problem and made it agnostic of the technology.
What I'm really keeping in that dictionary is <UIElement, ElementHost>. More
context: ElementHost is the way WPF provides to integrate WPF controls (they
all derive from UIElement) within Winforms apps.
Basically what I want to achieve (using other components in the equation) is
hiding the user the complexity of ElementHost to ease the Winforms to WPF
migrations.


I'm not 100% sure what you want to know or what you're trying to
achieve. Are you unclear on the semantics of WeakReferences?

WeakReferences permit collection of the inner reference, so certainly,
it will stop the dictionary keeping the component alive forever.
However, you'll still have to do periodic scans to clear out any dead
references, to actually remove items from the dictionary.


Yes I was not sure if having a WeakRef dictionary would be the solution. I
don't want the dictionary to keep a strong reference, hence prevent the GC
to collect the ElementHost even when this is not used any more in the UI. So
it seems that creating a WeakRef dictionary is the solution.
Regarding the periodic scans I can do that proactively. If you ask for a
specific key I get the WeakRef and check if the Target is null, if it is I
will remove it from the inner dictionary.

private Dictionary<TKey, WeakReference> inner = new Dictionary<TKey,
WeakReference>();

public TValue this[TKey] {
get {
 object target = wr.Target;
 if (target == null)
 {
    inner.Remove(key);
 }
 return target;
}

What do you think?

Thanks



On 4/9/07, Barry Kelly <[EMAIL PROTECTED]> wrote:

Matias Woloski <[EMAIL PROTECTED]> wrote:

> I have a *ComponentCatalog* class that manages a list of
> *System.ComponentModel.Component*. It's actually a façade for a
> Dictionary<string,Component>.
>

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to