How do I add Dispose functionality to a C# UserControl?(如何将 Dispose 功能添加到 C# UserControl?)
问题描述
我有一个实现 UserControl 的类.在 .NET 2005 中,会在 MyClass.Designer.cs 部分类文件中自动创建一个 Dispose 方法,如下所示:
I have a class which implements UserControl. In .NET 2005, a Dispose method is automatically created in the MyClass.Designer.cs partial class file that looks like this:
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
如果我想添加自己的 Dispose 功能,我应该把它放在哪里?由于生成了这个文件,我不想在这里添加代码并冒着被吹走的风险.
If I want to add my own Dispose functionality, where would I put it? Since this file is generated, I don't want to add code here and risk it getting blown away.
推荐答案
在这种情况下,我将生成的 Dispose
方法移动到主文件并扩展它.Visual Studio 尊重这一点.
In such a case I move the generated Dispose
method to the main file and extend it. Visual Studio respects this.
另一种方法是使用部分方法 (C# 3.0).
An other approach would be using a partial method (C# 3.0).
这篇关于如何将 Dispose 功能添加到 C# UserControl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将 Dispose 功能添加到 C# UserControl?


- WebMatrix WebSecurity PasswordSalt 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 输入按键事件处理程序 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01