我看到可能的内存泄漏,我正在使用一些代码来加载WF .NET Activity工具箱活动的图标。检索图标的建议方法是使用反射,但是在关闭对话框后,反射似乎会导致应用程序挂起到内存中的对象上。症状基本上是在重复打开和关闭对话框时应用程序大小继续增加(甚至在强制执行垃圾收集之后)。
任何想法,或者之前有人见过这个?代码如下:
private void CreateToolboxBitmapAttributeForActivity(AttributeTableBuilder builder, Type activityType, Type activityImageType, string imageName)
{
if (activityImageType != null && !string.IsNullOrEmpty(imageName))
{
Bitmap bitmap = LoadIconImage(activityImageType.Assembly, imageName);
if (bitmap != null)
{
Type tbaType = typeof(System.Drawing.ToolboxBitmapAttribute);
Type imageType = typeof(System.Drawing.Image);
ConstructorInfo constructor = tbaType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] { imageType, imageType }, null);
System.Drawing.ToolboxBitmapAttribute tba = constructor.Invoke(new object[] { bitmap, bitmap }) as System.Drawing.ToolboxBitmapAttribute;
builder.AddCustomAttributes(activityType, tba);
}
}
}
Bitmap的加载方式如下:
private Bitmap LoadIconImage(Assembly a, string imageName)
{
Bitmap bmp = null;
// attach to stream to the resource in the manifest
Stream imgStream = a.GetManifestResourceStream(imageName);
if (!(null == imgStream))
{
// create a new bitmap from this stream and
// add it to the arraylist
bmp = Bitmap.FromStream(imgStream) as Bitmap;
imgStream.Close();
imgStream.Dispose();
imgStream = null;
}
return bmp;
}
作为参考,我基本上使用此处详述的方法:Displaying .NET Framework 4 Built-In Workflow Activity Icons in a rehosted Workflow Designer