private static void AttachMethodToEvent( object sinkService, object sourceService, MethodInfo sinkMethodInfo, EventInfo sourceEventInfo, GetEventMethodDelegate delGev) { Delegate del = CreateDelegate(sinkService, sinkMethodInfo, sourceEventInfo); // Add or remove the delegate to the event // using the event's method retrieved using // the delGev GetEventMethodDelegate MethodInfo delAddMethodInfo = delGev(sourceEventInfo); Object[] delAddMethodArgs = { del }; try { delAddMethodInfo.Invoke(sourceService, delAddMethodArgs); } catch (TargetException) { Debug.Fail(UnexpectedException.Message); throw new UnexpectedException(); } catch (ArgumentException) { // Arguments should be right because the delegate is // the correct type for the event Debug.Fail(UnexpectedException.Message); throw new UnexpectedException(); } catch (TargetInvocationException) { // Not really sure Debug.Fail(UnexpectedException.Message); throw new UnexpectedException(); } catch (TargetParameterCountException) { // Arguments should be correct... Debug.Fail(UnexpectedException.Message); throw new UnexpectedException(); } catch (MethodAccessException) { Debug.Fail(UnexpectedException.Message); throw new UnexpectedException(); } catch (InvalidOperationException) { // I don't really know if this should be expected Debug.Fail(UnexpectedException.Message); throw new UnexpectedException(); } }
private static void AttachSinkToEvent( object sinkService, object sourceService, EventInfo sourceEventInfo, GetEventMethodDelegate delGev) { // Try to subscribe each method of the sink // to each event of the service Type sinkServiceType = sinkService.GetType(); MethodInfo[] sinkMethodInfoArray = sinkServiceType.GetMethods(); foreach (MethodInfo sinkMethodInfo in sinkMethodInfoArray) { AttachMethodToEvent(sinkService, sourceService, sinkMethodInfo, sourceEventInfo, delGev); } }
private static void AttachSinkToSource( object sinkService, object sourceService, GetEventMethodDelegate delGev) { Debug.Assert(null != sinkService); Debug.Assert(null != sourceService); // Type of service Type sourceServiceType = sourceService.GetType(); // Go through all the events in the service EventInfo[] sourceEventInfoArray = sourceServiceType.GetEvents(); foreach (EventInfo sourceEventInfo in sourceEventInfoArray) { AttachSinkToEvent(sinkService, sourceService, sourceEventInfo, delGev); } }
// TODO: These aren't correctly named since they can be used to both attach and remove private static void AttachSinkToAllSources( object sinkService, Stack<object> sourceServices, GetEventMethodDelegate delGev) { Debug.Assert(null != sinkService); // Catch everything try { // Try each current service foreach (object source in sourceServices) { Debug.Assert(null != source, "Shouldn't be possible for the list to contain null"); AttachSinkToSource(sinkService, source, delGev); } } catch { Debug.Fail(UnexpectedException.Message); throw new UnexpectedException(); } }