Class 级别对象上的 CA2000 警告
CA2000 warning on Class Level Object
我有一个在 class 级别声明的对象,它发出 CA2000 警告。我怎样才能摆脱下面代码中的 CA2000 警告?
public partial class someclass : Window
{
System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog()
{
AddExtension = true,
CheckFileExists = true,
CheckPathExists = true,
DefaultExt = "xsd",
FileName = lastFileName,
Filter = "XML Schema Definitions (*.xsd)|*.xsd|All Files (*.*)|*.*",
InitialDirectory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop),
RestoreDirectory = true,
Title = "Open an XML Schema Definition File"
};
}
警告是 - 警告 CA2000 在方法 'SIMPathFinder.SIMPathFinder()' 中,对象 'new OpenFileDialog()' 未沿所有异常路径处理。在对象 'new OpenFileDialog()' 的所有引用超出范围之前调用对象 System.IDisposable.Dispose。
CA2000 表示您的 class 实例拥有一个一次性对象,应在 class 实例超出范围之前将其释放以释放已用(非托管)资源。
执行此操作的常见模式是实现 IDisposable
接口和 protected virtual Dispose
方法:
public partial class someclass : Window, IDisposable // implement IDisposable
{
// shortened for brevity
System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
protected virtual void Dispose(bool disposing)
{
if (disposing)
dlg.Dispose(); // dispose the dialog
}
public void Dispose() // IDisposable implementation
{
Dispose(true);
// tell the GC that there's no need for a finalizer call
GC.SuppressFinalize(this);
}
}
阅读有关 Dispose-Pattern
的更多信息
附带说明:您似乎混合了 WPF 和 Windows 表单。您继承自 Window
(我认为是 System.Windows.Window
,因为您的问题被标记为 wpf),但请尝试使用 System.Windows.Forms.OpenFileDialog
.
混合使用这两个 UI 框架不是一个好主意。请改用 Microsoft.Win32.OpenFileDialog
。
我有一个在 class 级别声明的对象,它发出 CA2000 警告。我怎样才能摆脱下面代码中的 CA2000 警告?
public partial class someclass : Window
{
System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog()
{
AddExtension = true,
CheckFileExists = true,
CheckPathExists = true,
DefaultExt = "xsd",
FileName = lastFileName,
Filter = "XML Schema Definitions (*.xsd)|*.xsd|All Files (*.*)|*.*",
InitialDirectory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop),
RestoreDirectory = true,
Title = "Open an XML Schema Definition File"
};
}
警告是 - 警告 CA2000 在方法 'SIMPathFinder.SIMPathFinder()' 中,对象 'new OpenFileDialog()' 未沿所有异常路径处理。在对象 'new OpenFileDialog()' 的所有引用超出范围之前调用对象 System.IDisposable.Dispose。
CA2000 表示您的 class 实例拥有一个一次性对象,应在 class 实例超出范围之前将其释放以释放已用(非托管)资源。
执行此操作的常见模式是实现 IDisposable
接口和 protected virtual Dispose
方法:
public partial class someclass : Window, IDisposable // implement IDisposable
{
// shortened for brevity
System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
protected virtual void Dispose(bool disposing)
{
if (disposing)
dlg.Dispose(); // dispose the dialog
}
public void Dispose() // IDisposable implementation
{
Dispose(true);
// tell the GC that there's no need for a finalizer call
GC.SuppressFinalize(this);
}
}
阅读有关 Dispose-Pattern
的更多信息附带说明:您似乎混合了 WPF 和 Windows 表单。您继承自 Window
(我认为是 System.Windows.Window
,因为您的问题被标记为 wpf),但请尝试使用 System.Windows.Forms.OpenFileDialog
.
混合使用这两个 UI 框架不是一个好主意。请改用 Microsoft.Win32.OpenFileDialog
。