在VS2010中如何判断一个类需要被释放?

发布于 2024-12-18 11:26:32 字数 1455 浏览 0 评论 0原文

在 Visual Studio 2010 中是否有一种简单的方法可以知道类型是否需要被释放?

例如,我编写代码:

Collection<Prize> prizes = new Collection<Prize>();

我不知道是否需要调用 dispose。

我现在处理它的方式是单击Collection并按F12,寻找IDisposable

public class Collection<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable

然后我递归地深入到每个类,寻找如果有实现IDisposable

public class Collection<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
   public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable
      public interface ICollection<T> : IEnumerable<T>, IEnumerable
         public interface IEnumerable<T> : IEnumerable
            public interface IEnumerable    
         public interface IEnumerable
      public interface IEnumerable<T> : IEnumerable    
         public interface IEnumerable
      ...

注意:不要将示例与问题混淆。我可能有代码:

 SqlConnection conn = new SqlConnection();

然后我反复迭代祖先类型:

 公共密封类 SqlConnection :DbConnection、ICloneable
      公共抽象类 DbConnection :组件、IDbConnection、IDisposable

所以我发现这个类需要我调用Dispose。但如果我不必 F12 下降到东西中会更容易

Is there an easy way, in Visual Studio 2010, to know if a type needs to be disposed?

e.g. i write code:

Collection<Prize> prizes = new Collection<Prize>();

i don't know if i need to call dispose.

The way i handle it now is click on Collection and press F12, looking for IDisposable:

public class Collection<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable

then i recursively descend into each class, looking to see if any implement IDisposable:

public class Collection<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
   public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable
      public interface ICollection<T> : IEnumerable<T>, IEnumerable
         public interface IEnumerable<T> : IEnumerable
            public interface IEnumerable    
         public interface IEnumerable
      public interface IEnumerable<T> : IEnumerable    
         public interface IEnumerable
      ...

Note: Don't confuse the example with the question. i might have the code:

 SqlConnection conn = new SqlConnection();

where i then recusively iterate into ancestor types:

  public sealed class SqlConnection : DbConnection, ICloneable
      public abstract class DbConnection : Component, IDbConnection, IDisposable

So i've found that this class needs me to call Dispose. But it would be easier if i didn't have to F12 descend into stuff

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情场扛把子 2024-12-25 11:26:32

如果您有高级版或终极版代码分析规则CA1001:拥有一次性字段的类型应该bedisposable 将找到需要处置的类型。要启用代码分析,请转到项目属性并选择代码分析选项卡,选择“在生成时启用代码分析”,必须包含 Microsoft.Design 规则集才能运行 CA1001 规则。

Devexpress 的 Coderush 包含类似的功能。这是标准版的唯一选项。可能还有其他提供类似功能的加载项。

这在 Express 版本中无法完成。

If you have the premium or ultimate edition code analysis rule CA1001:Types that own disposable fields should be disposable will find types that need to be disposed. To enable code analysis go to the project properties and select the code analysis tab, select Enable Code Analysis on Build, the Microsoft.Design ruleset must be included for the CA1001 rule to run.

Coderush from Devexpress includes similar functionality. This is the only option for the standard edition. There may other add-ins that offer similar functionality.

This cannot be done in the express edition.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文