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?


基础教程推荐
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01