コードが呼び出されている時間を教えてくれる内部メッセージングシステムを作成したいと思っています。SystemMessageクラスにIDisposableを実装させるために、使いやすさを考えていました。
SystemMessageのコンストラクター中にタイムスタンプを設定し、Disposeが呼び出された場合、期間を把握できました。
問題は、オブジェクトをGC化したくないということです。MessageCollectionの一部として残しておいてほしい。
IDisposableの目的の機能を踏まずに、Usingステートメントの使いやすさを提供できるC#の別の構造はありますか?
Using (message = Collection.CreateNewMessage("FileDownlading"))
{
// I wonder how long it is taking me to download this file in production?
// Lets log it in a message and store for later pondering.
WebClass.DownloadAFile("You Know This File Is Great.XML");
}
// we fell out of the using statement, message will figure out how long
// it actually took to run.
// This was clean and easy to implement, but so wrong?