Была такая же проблема.Код от jimmy_keen не работал должным образом со старыми .NET.Решил это, написав вспомогательные методы:
public static void assertSubscribed<EventHandlerType>(object handler, object subscriber, string eventName = null) {
var inappropriate = false;
try {
if (!typeof (EventHandlerType).IsSubclassOf(typeof (Delegate)) ||
typeof (EventHandlerType).GetMethod("Invoke").ReturnType != typeof (void))
inappropriate = true;
} catch (AmbiguousMatchException) {
inappropriate = true;
} finally {
if (inappropriate) throw new Exception("Inappropriate Delegate: " + typeof (EventHandlerType).Name);
}
var handlerField = subscriber.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
.First(h => h.FieldType.IsInstanceOfType(handler));
var handlerInstance = handlerField == null ? null : handlerField.GetValue(subscriber);
var eventField = handlerInstance == null ? null : handlerInstance.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
.First(f => (f.FieldType.IsAssignableFrom(typeof (EventHandlerType)) &&
(eventName == null || eventName.Equals(f.Name))));
var eventInstance = eventField == null ? null : (Delegate)eventField.GetValue(handlerInstance);
var subscribedMethod = eventInstance == null
? null
:eventInstance.GetInvocationList().FirstOrDefault(
d => d.Method.DeclaringType != null && d.Method.DeclaringType.IsInstanceOfType(subscriber));
Assert.That(subscribedMethod, Is.Not.Null);
}
"Not" метод:
public static void assertNotSubscribed<EventHandlerType>(object handler, object subscriber, string eventName = null) {
var inappropriate = false;
try {
if (!typeof (EventHandlerType).IsSubclassOf(typeof (Delegate)) ||
typeof (EventHandlerType).GetMethod("Invoke").ReturnType != typeof (void))
inappropriate = true;
} catch (AmbiguousMatchException) {
inappropriate = true;
}
if (inappropriate) return;
var handlerField = subscriber.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
.First(h => h.FieldType.IsInstanceOfType(handler));
var handlerInstance = handlerField == null ? null : handlerField.GetValue(subscriber);
var eventField = handlerInstance == null ? null : handlerInstance.GetType()
.GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
.First(f => (f.FieldType.IsAssignableFrom(typeof (EventHandlerType)) &&
(eventName == null || eventName.Equals(f.Name))));
var eventInstance = eventField==null?null:(Delegate) eventField.GetValue(handlerInstance);
var subscribedMethod = eventInstance == null
? null
: eventInstance.GetInvocationList().FirstOrDefault(
d => d.Method.DeclaringType != null && d.Method.DeclaringType.IsInstanceOfType(subscriber));
Assert.That(subscribedMethod, Is.Null);
}
И вызов:
assertSubscribed<EventHandler>(handler, subscriber);
assertNotSubscribed<EventHandler>(handler, subscriber);
assertSubscribed<EventHandler>(handler, subscriber, "myRealWorldEvent");
assertNotSubscribed<EventHandler>(handler, subscriber, "myRealWorldEvent");
Не мешайте мне по стилю кодасоглашение, но этот метод выглядит достаточно компактным.