我有以下代码
Type type = ...
var events=type.GetEvents(
BindingFlags.DeclaredOnly
| BindingFlags.Instance
| BindingFlags.Public).ToList();
但是,这也会返回我在父接口上声明的事件。例如
两个都
UIElement
ContentElement
实施
IInputElement
它定义了事件
//
// Summary:
// Occurs when the mouse pointer moves while the mouse pointer is over the element.
event MouseEventHandler PreviewMouseMove;
但是上面对 GetEvents 的调用以及上面设置的所有绑定标志都会返回接口的事件以及两个具体类。
如何从 GetEvents 中过滤掉父接口上定义的事件?
请注意,我正在为每个事件生成扩展方法,就像这样
public static
IObservable<EventPattern<MouseButtonEventArgs>>
PreviewMouseLeftButtonDownObserver(this IInputElement This){
return Observable
.FromEventPattern
<MouseButtonEventHandler, MouseButtonEventArgs>
( h => This.PreviewMouseLeftButtonDown += h
, h => This.PreviewMouseLeftButtonDown -= h);
}
所以我只做了事件的根定义,而不是派生的虚拟或接口实现。