I often write code like that:
MyObject property;
MyObject Property
{
get { return property; }
set {
if (property != null)
property.Changed -= property_Changed; // unsubscribe from the old value
property = value;
property.Changed += property_Changed; // subscribe to the new value
}
}
I’m looking for an elegant way to make the unsubscribing automatic.
Any ideas?
Perhaps using Extensions Methods might be what you are looking for.
You could try something like this.