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?
基础教程推荐
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- WPF 模态进度窗口 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
