Inv*_*ion 12 c# events exception
是否可以阻止多个订阅者订阅活动?
我已经创建了一个快速示例代码片段给我的问题一些上下文但不幸的是我现在无法测试它,因为我不在我的VS机器上.
目标是:
这可能吗?
public delegate List<IBaseWindow> GetWindowListDelegate();
public static event GetWindowListDelegate GetWindowListEvent;
public List<IBaseWindow> GetWindowList() {
if (GetWindowListEvent == null) {
return new List<IBaseWindow>();
}
return GetWindowListEvent();
}
Run Code Online (Sandbox Code Playgroud)
注意:我使用的是.NET 3.5 sp1.
And*_*are 15
听起来您不需要事件 - 只需公开委托本身并允许调用者自己设置委托引用.
您可以使用事件访问器来完成此任务.类似于以下内容:
private EventHandler _h;
public event EventHandler H {
add {
if (...) { // Your conditions here.
// Warning (as per comments): clients may not
// expect problems to occur when adding listeners!
_h += value;
}
}
remove {
_h -= value;
}
}
Run Code Online (Sandbox Code Playgroud)
正如安德鲁指出的那样,你并不需要事件来完成这一任务.你需要它们有什么特别的原因吗?
只是为了完成John的答案,这里是一个只允许一个处理程序的事件的工作实现:
class Foo
{
private EventHandler _bar;
public event EventHandler Bar
{
add
{
if (_bar != null || value.GetInvocationList().Length > 1)
{
throw new InvalidOperationException("Only one handler allowed");
}
_bar = (EventHandler)Delegate.Combine(_bar, value);
}
remove
{
_bar = (EventHandler)Delegate.Remove(_bar, value);
}
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,公开委托而不是事件不会阻止多个处理程序:由于.NET委托是多播的,因此一个委托可以表示对多个方法的调用.但是,您可以将委托作为属性公开,并在setter中执行与上面代码中相同的检查.
无论如何,正如其他人所指出的那样,防止一个事件的多个处理程序可能不是一个好主意......对于使用它的开发人员来说会非常困惑.