次の疑似コードは、DLL が私が制御していないコードによってホストされているときに、自分でクリーンアップするという私の目標を達成しますか?
より具体的には、静的コンストラクターで作成されたオブジェクトをクリーンアップするにはどうすればよいですか?
ディスポーザブルでファイナライズを抑制する必要がありますか?
ホストが呼び出さなくても、コンパイラまたは何かが IDisposable を呼び出すことが保証されていますか?
疑似コード:
public class MyDLL : SomeHostICantControl, IDisposable
{
public SpinLock MyDictionarySpinlock = new Spinlock; // approximate syntax
public static Dictionary<string, string> MyDictionary = null;
public static Timer MyCleanUpTimer = null;
public static MyDLL()
{
// Set up Cache Here ... how do I dispose of it?
MyDictionary = new Dictionary<string, string>();
// remove old data from the dictionary, use spinlock to sync writes
// or use concurrent dictionary in .NET 4
MyCleanUpTimer = new Timer(); // run every hour
}
// many instances will be created and disposed of. I have no control when or how often
public void MyDll()
{
this.MyHostEvent += New Event Handler....
}
//.. some event handler code here
public void Dispose()
{
this.MyHostEvent -= Event Handler.;
// Do I need to suppressFinalize here?
}
//~MyDll()
//{
// Is this the right place to clean up after my constuctor?
//}
}