当对象设置为null时...删除事件 [英] When objects are set to null...removing events

查看:90
本文介绍了当对象设置为null时...删除事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能听起来非常基本,但是当你的对象在对象中设置为null时,你能陷阱吗?


我创建了一个类从构造函数中传递的对象中注册一个事件

。当我的对象被销毁时,我希望我的对象

取消注册此事件。如果我不是那么对象将永远不会被销毁,直到我在构造函数中传递的对象被销毁。


我已经实现了Dispose( ),Dispose(bool)和~Finalize方法,

其中finalize调用Dispose。但只有当

对象收集了垃圾时才会调用它。


如果使用我的对象的开发人员使用以下语句之一,

我希望我的对象取消注册事件:


myObject = null;





myObject = new myObject()


这可能吗?如果没有,我可以强迫开发人员调用

Dispose方法吗?


我想其他人已经遇到这种情况,你做了什么?


感谢任何可以提供帮助的人。

This may sound very elementary, but can you trap when your object is
set to null within the object?

I have created a class that registers an event from an object passed
in the constructor. When my object is destroyed, I want my object to
un-register this event. If I don''t then the object would never be
destroyed until the object I passed in the constructor is destroyed.

I have implemented a Dispose(), Dispose(bool), and ~Finalize method,
where the finalize calls Dispose. But this is only called when the
object is Garbage Collected.

If the developer using my object uses one of the following statements,
I want my object to unregister the event:

myObject = null;

or

myObject = new myObject()

Is this possible? If not, can I force the developer to call the
Dispose method?

I imagine other have run into this scenario, what have you done?

Thanks to anyone that can help.

推荐答案

表达式myObject = null将myObject引用为null引用(不是
引用任何对象),GC将在下一次迭代时收集它。如果

开发人员将对象确定为IDisposable,则应使用

使用声明

http:// msdn2.microsoft.com/en-us/library/yh598w02.aspx


顺便提一下,你应该提供GC.SuppressFinalize(this)。在

..Dispose方法中抑制不必要的Finalize调用。


问候,Alex Meleta

Blog :: http://devkids.blogspot.com

-----原创消息-----

来自:MikeT [mailto:th ********** @ gmail.com]

发布于:4月周三25,2007 3:36 PM

发布至:microsoft.public.dotnet.languages.csharp

对话:当对象设置为null时...删除事件

主题:当对象设置为null时...删除事件


这可能听起来非常基本,但是当你的对象是
时你能陷阱吗? b $ b在对象中设置为null?

我创建了一个类来记录构造函数中传递的对象的事件

。当我的对象被销毁时,我希望我的对象

取消注册此事件。如果我不是那么对象将永远不会被销毁,直到我在构造函数中传递的对象被销毁。


我已经实现了Dispose( ),Dispose(bool)和~Finalize方法,

其中finalize调用Dispose。但只有当

对象收集了垃圾时才会调用它。


如果使用我的对象的开发人员使用以下语句之一,

我希望我的对象取消注册事件:


myObject = null;





myObject = new myObject()


这可能吗?如果没有,我可以强迫开发人员调用

Dispose方法吗?


我想其他人已经遇到这种情况,你做了什么?


感谢任何可以提供帮助的人。

The expression "myObject = null" refs myObject to null reference (not
refer to any object) and GC will collect it by the next iteration. If
developer determinates an object as IDisposable he should use the
"using" statement
(http://msdn2.microsoft.com/en-us/library/yh598w02.aspx)

By the way u should provide the "GC.SuppressFinalize(this)" in the
..Dispose method to suppress unnecessary Finalize call.

Regards, Alex Meleta
Blog:: http://devkids.blogspot.com
-----Original Message-----
From: MikeT [mailto:th**********@gmail.com]
Posted At: Wednesday, April 25, 2007 3:36 PM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: When objects are set to null...removing events
Subject: When objects are set to null...removing events

This may sound very elementary, but can you trap when your object is
set to null within the object?

I have created a class that registers an event from an object passed
in the constructor. When my object is destroyed, I want my object to
un-register this event. If I don''t then the object would never be
destroyed until the object I passed in the constructor is destroyed.

I have implemented a Dispose(), Dispose(bool), and ~Finalize method,
where the finalize calls Dispose. But this is only called when the
object is Garbage Collected.

If the developer using my object uses one of the following statements,
I want my object to unregister the event:

myObject = null;

or

myObject = new myObject()

Is this possible? If not, can I force the developer to call the
Dispose method?

I imagine other have run into this scenario, what have you done?

Thanks to anyone that can help.


MikeT写道:
MikeT wrote:

这可能听起来非常基本,但是当你的对象在对象中设置为null时,你能陷阱吗?
This may sound very elementary, but can you trap when your object is
set to null within the object?



你不能。

You cannot.


我创建了一个类来记录来自传递的对象的事件/>
在构造函数中。当我的对象被销毁时,我希望我的对象

取消注册此事件。
I have created a class that registers an event from an object passed
in the constructor. When my object is destroyed, I want my object to
un-register this event.



在.NET中实现此语义的惯用方法是实现

IDisposable,但它也要求您的类的用户玩

,必要时调用Dispose(),或者只在''using''块中使用

实例。

The idiomatic way to implement this semantic in .NET is to implement
IDisposable, but it also requires that the users of your class play
along, by calling Dispose() as necessary, or alternatively using
instances of your class only in ''using'' blocks.


我已经实现了Dispose(),Dispose(bool)和~Finalize方法,

,其中finalize调用Dispose。但这仅在

对象为Garbage Collected时调用。
I have implemented a Dispose(), Dispose(bool), and ~Finalize method,
where the finalize calls Dispose. But this is only called when the
object is Garbage Collected.



终结器适用于非常有限的场景,例如包装非托管

资源。除非您在操作系统或本地库中与

a非常低级别进行交互,否则您无需执行终结器。


你应该在你的课上实现IDisposable;这涉及到一个

Dispose()方法。 Dispose()方法应该调用你的受保护的

虚拟Dispose(bool)方法传递''true'',你应该

在Dispose中取消注册你的事件处理程序( bool)方法大致类似于

这个:


--- 8< ---

protected virtual void Dispose(bool disposing) )

{

if(处理)

{

someOtherObject.TheEvent - = MyHandler;

}

}

---> 8 ---


如果有这样的事情.NET中的弱代表,你的后代

会更有可能,但由于GC,它仍然是不确定的


Finalizers are for very limited scenarios, such as wrapping unmanaged
resources. Unless you''re interacting with the OS or a native library at
a very low level, you don''t ever need to implement a finalizer.

You should implement IDisposable on your class; this involves having a
Dispose() method. The Dispose() method should call your protected
virtual Dispose(bool) method passing along ''true'', and you should
unregister your event handler in the Dispose(bool) method roughly like
this:

---8<---
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
someOtherObject.TheEvent -= MyHandler;
}
}
--->8---

If there were such a thing as weak delegates in .NET, what your after
would be somewhat more possible, but it would still be non-deterministic
due to GC.


如果使用我的对象的开发人员使用以下语句之一,

我希望我的对象取消注册事件:


myObject = null;





myObject = new myObject()


这是可能?
If the developer using my object uses one of the following statements,
I want my object to unregister the event:

myObject = null;

or

myObject = new myObject()

Is this possible?



No.

No.


如果没有,我可以强迫开发者拨打

处理方法?
If not, can I force the developer to call the
Dispose method?



No.

No.


我想其他人已遇到这种情况,你做了什么?
I imagine other have run into this scenario, what have you done?



始终处置实现IDisposable的对象,并在需要处理的对象上实现

IDisposable。


- Barry


-
http://barrkel.blogspot.com/


Alex Meleta写道:
Alex Meleta wrote:

表达式myObject = null将myObject引用为null引用(不是
引用任何对象),GC将在下一次迭代时收集它。
The expression "myObject = null" refs myObject to null reference (not
refer to any object) and GC will collect it by the next iteration.



它不会,因为OP订阅了一个活动。事件处理程序中的代表

实例将使对象保持活动状态,从而阻止

集合。


- Barry


-
http://barrkel.blogspot。 com /


这篇关于当对象设置为null时...删除事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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