如何将 Dispose 功能添加到 C# UserControl? [英] How do I add Dispose functionality to a C# UserControl?

查看:17
本文介绍了如何将 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆