public void CallingAnInterceptedMethod_InterceptorThatChangesTheInputParameters_GetsForwardedToTheInterceptee() { // Arrange string expectedValue = "XYZ"; var container = new Container(); var interceptee = new WithOutAndRef(); var interceptor = new DelegateInterceptor(); interceptor.Intercepting += invocation => { invocation.Arguments[0] = expectedValue; }; container.RegisterInstance <IWithOutAndRef>(interceptee); container.InterceptWith(interceptor, IsInterface); var intercepted = container.GetInstance <IWithOutAndRef>(); // Act string refValue = "Something different"; string unused; intercepted.Operate(ref refValue, out unused); // Assert Assert.AreEqual(expectedValue, interceptee.SuppliedRefValue); }
public void CallingAnInterceptedMethod_InterceptorThatChangesAnOutputParameter_OutputParameterFlowsBackToTheCaller() { // Arrange string expectedOutValue = "KLM"; var container = new Container(); var interceptee = new WithOutAndRef(); var interceptor = new DelegateInterceptor(); interceptor.Intercepted += invocation => { invocation.Arguments[1] = expectedOutValue; }; container.RegisterInstance <IWithOutAndRef>(interceptee); container.InterceptWith(interceptor, IsInterface); var intercepted = container.GetInstance <IWithOutAndRef>(); // Act string unused = null; string actualOutValue; intercepted.Operate(ref unused, out actualOutValue); // Assert Assert.AreEqual(expectedOutValue, actualOutValue); }
protected override Object ResolveListenerIntern(Object listener) { listener = base.ResolveListenerIntern(listener); if (ListenerMethodName != null) { Type parameterType = addMethod.GetParameters()[0].ParameterType; MethodInfo[] methodsOnExpectedListenerType = parameterType.GetMethods(BindingFlags.Instance | BindingFlags.Public); HashMap <MethodInfo, MethodInfo> mappedMethods = new HashMap <MethodInfo, MethodInfo>(); foreach (MethodInfo methodOnExpectedListenerType in methodsOnExpectedListenerType) { ParameterInfo[] parameters = methodOnExpectedListenerType.GetParameters(); Type[] types = new Type[parameters.Length]; for (int a = parameters.Length; a-- > 0;) { types[a] = parameters[a].ParameterType; } MethodInfo method = listener.GetType().GetMethod(ListenerMethodName, types); if (method != null) { mappedMethods.Put(methodOnExpectedListenerType, method); } } IInterceptor interceptor = new DelegateInterceptor(listener, mappedMethods); listener = ProxyFactory.CreateProxy(parameterType, interceptor); } return(listener); }
/// <summary> /// Replaces delegate parameters with call interceptors. /// </summary> /// <param name="message">Remoting message</param> /// <returns>Parameters</returns> private object[] InterceptDelegateParameters(IMethodCallMessage message) { object[] result = new object[message.ArgCount]; ParameterInfo[] paramDefs = message.MethodBase.GetParameters(); for (int i = 0; i < message.ArgCount; i++) { object arg = message.Args[i]; if (arg != null && typeof(Delegate).IsAssignableFrom(arg.GetType())) { DelegateInterceptor interceptor = new DelegateInterceptor() { ClientDelegate = arg }; result[i] = interceptor; } else { Type argType = paramDefs[i].ParameterType; Type handledType; ISerializationHandler handler; _connection.SerializationHandling.FindMatchingSerializationHandler(argType, out handledType, out handler); if (handler != null) { byte[] raw = handler.Serialize(arg); result[i] = new CustomSerializationContainer(handledType, argType, raw); } else // 1:1 result[i] = arg; } } return result; }
/// <summary> /// Handles subscription to events. /// </summary> /// <param name="methodCallMessage"><see cref="IMethodCallMessage"/> to process.</param> /// <param name="methodInfo"><see cref="MethodInfo"/> for the method being called.</param> /// <returns><see cref="ReturnMessage"/>, if the call is processed successfully, otherwise, false.</returns> private ReturnMessage HandleEventSubscription(IMethodCallMessage methodCallMessage, MethodInfo methodInfo) { // Check for delegate parameters in properties and events if (methodInfo.ReturnType.Equals(typeof(void)) && (methodCallMessage.MethodName.StartsWith("set_") || methodCallMessage.MethodName.StartsWith("add_")) && methodCallMessage.InArgCount == 1 && methodCallMessage.ArgCount == 1 && methodCallMessage.Args[0] != null && typeof(Delegate).IsAssignableFrom(methodCallMessage.Args[0].GetType())) { // Get client delegate var receiveMethodDelegate = methodCallMessage.GetArg(0) as Delegate; var eventFilter = default(IEventFilter); // Get event filter, if it is attached ExtractEventHandlerDetails(ref receiveMethodDelegate, ref eventFilter); // Trim "set_" or "add_" prefix string propertyName = methodCallMessage.MethodName.Substring(4); // Create delegate interceptor and correlation info var wiring = new DelegateInterceptor() { ClientDelegate = receiveMethodDelegate, SynchronizationContext = _synchronizationContext }; var correlationInfo = new DelegateCorrelationInfo() { IsEvent = methodCallMessage.MethodName.StartsWith("add_"), DelegateMemberName = propertyName, ClientDelegateInterceptor = wiring, EventFilter = eventFilter }; OptionalAsync(ZyanSettings.LegacyBlockingSubscriptions, () => AddRemoteEventHandlers(new List<DelegateCorrelationInfo> { correlationInfo })); // Save delegate correlation info lock (_delegateCorrelationSet) { _delegateCorrelationSet.Add(correlationInfo); } return new ReturnMessage(null, null, 0, methodCallMessage.LogicalCallContext, methodCallMessage); } // This method doesn't represent event subscription return null; }
public void CallingAnInterceptedMethod_InterceptorThatChangesAnOutputParameter_OutputParameterFlowsBackToTheCaller() { // Arrange string expectedOutValue = "KLM"; var container = new Container(); var interceptee = new WithOutAndRef(); var interceptor = new DelegateInterceptor(); interceptor.Intercepted += invocation => { invocation.Arguments[1] = expectedOutValue; }; container.RegisterSingleton<IWithOutAndRef>(interceptee); container.InterceptWith(interceptor, IsInterface); var intercepted = container.GetInstance<IWithOutAndRef>(); // Act string unused = null; string actualOutValue; intercepted.Operate(ref unused, out actualOutValue); // Assert Assert.AreEqual(expectedOutValue, actualOutValue); }
public void CallingAnInterceptedMethod_InterceptorThatChangesTheInputParameters_GetsForwardedToTheInterceptee() { // Arrange string expectedValue = "XYZ"; var container = new Container(); var interceptee = new WithOutAndRef(); var interceptor = new DelegateInterceptor(); interceptor.Intercepting += invocation => { invocation.Arguments[0] = expectedValue; }; container.RegisterSingleton<IWithOutAndRef>(interceptee); container.InterceptWith(interceptor, IsInterface); var intercepted = container.GetInstance<IWithOutAndRef>(); // Act string refValue = "Something different"; string unused; intercepted.Operate(ref refValue, out unused); // Assert Assert.AreEqual(expectedValue, interceptee.SuppliedRefValue); }