【发布时间】:2015-06-18 01:53:27
【问题描述】:
我需要一些关于这种场景的最佳实践的建议。我搜索了但没有找到任何令人满意的答案。
我们在 winforms 项目中使用了第 3 方 (.net) DLL。它引发了一些事件,我们订阅了。问题是,我需要在课堂上明确取消订阅这些事件吗?
顺便说一句,我们都使用 .net 框架 4。感谢您的建议。
一些示例代码...
public class MyClientCode: IDisposable
{
private readonly 3rdParty.TheirClass _theirClass;
public MyClientCode()
{
_theirClass = new _theirClass()
_theirClass.ReadData += ReadDataEvent;
}
public void SomeOtherMethod()
{
//some other code
}
public void ReadDataEvent()
{
//some code
}
public void Dispose()
{
_theirClass.ReadData -= ReadDataEvent;
}
}
在按钮点击事件中,我会...
MyClientCode code = new MyClientCode();
code.SomeOtherMethod();
【问题讨论】:
-
不,假设 _theirClass 从未在 MyClientCode 类之外使用。你从来没有把它传出去吗?
-
是的 _theirClass 是私有的