c# - WPF代码分析: CA1001 Types that own disposable fields should be disposable

标签 c# wpf code-analysis fxcop

在我的 WPF 应用程序代码中,我收到以下警告:

CA1001 Types that own disposable fields should be disposable Implement IDisposable on 'MainWindow' because it creates members of the following IDisposable types: 'BackgroundWorker', 'DataTable'. If 'MainWindow' has previously shipped, adding new members that implement IDisposable to this type is considered a breaking change to existing consumers. yesMonitor MainWindow.xaml.cs 38

主窗口代码:

public partial class MainWindow : Window
{
    // Some code..    
}

出现这些警告的原因是什么?

最佳答案

忽略此警告是安全的。

Backgroundworker 和 DataTable 都出于正式原因实现了 IDisposable,它们实际上并不需要它。

此外,您的 MainWindow 具有(定义)应用程序的生命周期,因此无论如何都不会有资源泄漏。

如果您想要形式上正确并遵守所有规则,那么只需将 IDisposable 添加到您的 MainWindow 类中。有一个片段。

关于c# - WPF代码分析: CA1001 Types that own disposable fields should be disposable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30977812/

相关文章:

c# - 不存在的通用参数的单元测试

wpf - 使用 C# 在 WPF 中使 DataGrid 列标题可排序

c# - 调试时不要中断

wpf - 如何删除所有控件的全局FocusVisualStyle?

python - 使用 Python 代码覆盖工具理解和修剪大型库的回溯源代码

c# - 在 Roslyn 中解析参数类型

dependency-injection - 在实现 IDependencyResolver 和 IDependencyScope 的类中应如何编码 Dispose?

java - Unity C# 和 Layar SDK Java - AndroidJavaException

c# - TryCatch 与 TryParse 的优缺点

c# - ASP.NET Core Route Tag Helper 使用 Route?