Is it possible to detect if event has any listeners? (I need to dispose my event provider object, if nobody needs it)
是否可以检测事件是否有任何听众? (如果没有人需要,我需要处理我的事件提供者对象)
3 个解决方案
#1
21
Assume the class is in a 3rd party library and it can't be modified:
假设该类位于第三方库中,并且无法修改:
public class Data
{
public event EventHandler OnSave;
//other members
}
In your program:
在你的程序中:
Data d = new Data();
d.OnSave += delegate { Console.WriteLine("event"); };
var handler = typeof(Data).GetField("OnSave", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(d) as Delegate;
if (handler == null)
{
//no subscribers
}
else
{
var subscribers = handler.GetInvocationList();
//now you have the subscribers
}
#2
4
You can check if event is != null.
您可以检查事件是否为!= null。
By the way, in C# you need this check each time you raise an event:
顺便说一下,在C#中,每次举办活动时都需要进行检查:
if (TheEvent != null) {
TheEvent(this, e);
}
and the reason is exactly to check if the event has any listener.
原因是检查事件是否有任何监听器。
EDIT
Since you can't access TheEvent from outside the class, you could implement a method that does the check:
编辑因为您无法从类外部访问TheEvent,您可以实现一个执行检查的方法:
public class TheClass {
public bool HasEventListeners() {
return TheEvent != null;
}
}
#3
0
void Main()
{
Console.WriteLine(ContainsOnSomethingEvent()); // false
OnSomething += (o,e) => {};
Console.WriteLine(ContainsOnSomethingEvent()); // true
}
EventHandler mOnSomething;
event EventHandler OnSomething {
add { mOnSomething = (EventHandler)EventHandler.Combine(mOnSomething, value); }
remove { mOnSomething = (EventHandler)EventHandler.Remove(mOnSomething, value); }
}
public bool ContainsOnSomethingEvent() {
return mOnSomething != null && mOnSomething.GetInvocationList().Length > 0;
}
#1
21
Assume the class is in a 3rd party library and it can't be modified:
假设该类位于第三方库中,并且无法修改:
public class Data
{
public event EventHandler OnSave;
//other members
}
In your program:
在你的程序中:
Data d = new Data();
d.OnSave += delegate { Console.WriteLine("event"); };
var handler = typeof(Data).GetField("OnSave", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(d) as Delegate;
if (handler == null)
{
//no subscribers
}
else
{
var subscribers = handler.GetInvocationList();
//now you have the subscribers
}
#2
4
You can check if event is != null.
您可以检查事件是否为!= null。
By the way, in C# you need this check each time you raise an event:
顺便说一下,在C#中,每次举办活动时都需要进行检查:
if (TheEvent != null) {
TheEvent(this, e);
}
and the reason is exactly to check if the event has any listener.
原因是检查事件是否有任何监听器。
EDIT
Since you can't access TheEvent from outside the class, you could implement a method that does the check:
编辑因为您无法从类外部访问TheEvent,您可以实现一个执行检查的方法:
public class TheClass {
public bool HasEventListeners() {
return TheEvent != null;
}
}
#3
0
void Main()
{
Console.WriteLine(ContainsOnSomethingEvent()); // false
OnSomething += (o,e) => {};
Console.WriteLine(ContainsOnSomethingEvent()); // true
}
EventHandler mOnSomething;
event EventHandler OnSomething {
add { mOnSomething = (EventHandler)EventHandler.Combine(mOnSomething, value); }
remove { mOnSomething = (EventHandler)EventHandler.Remove(mOnSomething, value); }
}
public bool ContainsOnSomethingEvent() {
return mOnSomething != null && mOnSomething.GetInvocationList().Length > 0;
}