C#:WeakReference的通知之前收集?通知、WeakReference

2023-09-03 16:14:36 作者:瓶儿的盖儿

在C#/。NET,有没有什么办法让一个通知之前指向的对象由弱引用被破坏?基本上,我想允许被收集的对象,但做一些合适的对象之前被销毁,而无需修改code添加析构函数(因为我不知道到底是什么类型的对象将与我的$ C被起诉$ C)。

In C#/.NET, is there any way to get a notification before the object pointed to by a weak reference is destructed? Basically, I want to allow an object to be collected, but do something right before the object is destroyed, without modifying code to add destructors (since I won't know exactly what types of objects will be sued with my code).

谢谢, 罗伯特·

推荐答案

您不能这样做。 但是你可以做的是,当GC将至看(还有在CLR v3.5Sp1,让你这样做,GCNotifications新的GC的API)

You can't do that. However what you can do is to watch when a GC is approaching ( there are new GC APIs in CLR v3.5Sp1 that allows you to do so, GCNotifications )