WeakReference and event handling

前端 未结 4 2211
鱼传尺愫
鱼传尺愫 2020-12-14 00:53

Is it a good practice to implement event handling through WeakReference if that event is the only thing holding the reference and that we would need the object to be garbage

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-14 01:04

    Weak references in their own right, don't solve the problem as the delegate holds the reference. In the Composite Application Library which ships with Prism (www.microsoft.com/compositewpf) there is a WeakDelegate class that you could pull from the source. The WeakDelegate basically ues reflection and creates a delegate only for a moment in time and then releases it, thereby no holding any pointers. Within CAL it is used by the EventAggregator class, but you are free to rip it out for your own usage as it is under MS-PL.

提交回复
热议问题