代码实践,Microsoft可靠性CA2000帮助... [英] Code Practices, Microsoft Reliability CA2000 Help...

查看:61
本文介绍了代码实践,Microsoft可靠性CA2000帮助...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下一段代码仅供参考,并面临着以下问题:运行代码分析工具时出现错误CA200

CA2000:Microsoft.Reliability:在方法中,在对对象"newCol"的所有引用都超出范围之前,调用System.IDisposable.Dispose.

I have the following piece of code, just for reference, and im facing the problem that upon running the code Analysis tool i get the error CA200

CA2000 : Microsoft.Reliability : In method , call System.IDisposable.Dispose on object ''newCol'' before all references to it are out of scope.

Dim newCol As New ColumnHeader
newCol.TextAlign = 'whatever value
newCol.Text = 'whatever value
newCol.Tag = 'whatever value
newCol.Width = 'whatever value

ListView1.Columns.Add(newCol)



我想解决这种情况或类似情况,因为明显的原因,我不能在将newcol.Dispose()添加到ListView后使用它,也不能使用using语句.

最佳的代码实践是什么?我缺少的是什么?



And i was wondering to to deal with this, or similar situations, that i can not use newcol.Dispose() after adding it to the ListView, for obvious reasons, nor i can use the using statements.

What is the best code practice for this? what I''m missing?

推荐答案

要了解的重要一点是,这些警告仅仅是警告.它们不能满足所有情况,有时可能过于宽泛,因为它们在代码中寻找要处理的情况,而不是间接寻址的30个级别.在这种情况下,列表视图的处置应在内部管理列表视图框架分配的所有资源的处置.

如果我是你,我可能会用SuppressMessage属性禁止显示此消息.
The important thing to understand is that these warnings are, just that, warnings. They don''t cater for all cases, and can be too broad brush at times because they are looking in your code for the case to be handled, not something that may be 30 levels of indirection away. In this case, disposal of the listview should internally manage the disposal of all resources allocated by the listview framework.

If I were you, I''d probably suppress this message with the SuppressMessage attribute.


这篇关于代码实践,Microsoft可靠性CA2000帮助...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆