處理ImageList
對象的適當方法是什麼?處理ImageList
假設我和private ImageList imageList
成員有一些類。現在,在某些時刻,我執行下面的代碼:
// Basically, lazy initialization.
if (imageList == null)
{
imageList = new ImageList();
Image[] images = Provider.CreateImages(...);
foreach (var image in images)
{
// Does the 'ImageList' perform implicit copying here
// or does it aggregate a reference?
imageList.Images.Add(image);
// Do I need to do this?
//image.Dispose();
}
}
return imageList;
在同一個班我有Dispose
方法實現,其執行方式如下:
public void Dispose()
{
if (!disposed)
{
// Is this enough?
if (imageList != null)
imageList.Dispose();
disposed = true;
}
}
我敢肯定有這段代碼存在一些潛在的問題,所以請你幫我把它改正。
相關:[ImageList中:處置原始圖像從列表中刪除它(http://stackoverflow.com/questions/17639237/imagelist-disposing-the-original-image-removes-it-from-列表)。在[ImageList'中的'originals'字段的參考源中的註釋](https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/ImageList.cs,75)也值得一讀。 – jrh 2016-10-11 18:34:58