/// <summary> /// Invokes the message. /// </summary> /// <param name="msg">The message.</param> /// <returns>The result of invocation.</returns> public override IMessage Invoke(IMessage msg) { IConstructionCallMessage constructorCall = msg as IConstructionCallMessage; if (constructorCall != null) { if (this.target == null) { MethodInfo factoryMethodInfo = Utility.GetInheritedMethod(constructorCall.ActivationType, DynamicConstructionAttribute.InstantiateMethodName, new[] { typeof(Type), typeof(object[]) }); if (factoryMethodInfo == null) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Cannot create an instance of type '{0}' because there is no '{1}' static method defined in its inheritance hierarchy. See '{2}' methods for an example of corresponding method signature.", constructorCall.ActivationType.AssemblyQualifiedName, DynamicConstructionAttribute.InstantiateMethodName, typeof(LiveTest).AssemblyQualifiedName)); } if (!DynamicConstructionAttribute.EnterInstantiationPhase()) { throw new InvalidOperationException("Error in instantiation workflow. Object is already in the middle of instantiation when another instance of the object is created."); } try { this.target = (LiveTest)factoryMethodInfo.Invoke(null, new object[] { constructorCall.ActivationType, constructorCall.Args }); } finally { DynamicConstructionAttribute.LeaveInstantiationPhase(); } } return(EnterpriseServicesHelper.CreateConstructionReturnMessage(constructorCall, (MarshalByRefObject)this.GetTransparentProxy())); } IMethodCallMessage methodCall = msg as IMethodCallMessage; if (methodCall != null) { methodCall = new MethodCall(methodCall); MethodCallEventArgs eventArgs = typeof(LiveTest).IsAssignableFrom(methodCall.MethodBase.DeclaringType) ? new MethodCallEventArgs(Interlocked.Increment(ref methodCallId), methodCall.MethodBase, methodCall.Args) : null; if (eventArgs != null) { this.target.OnBeforeMethodCall(this.target, eventArgs); } IMessage result = RemotingServices.GetRealProxy(this.target).Invoke(methodCall); if (eventArgs != null) { this.target.OnAfterMethodCall(this.target, eventArgs); } return(result); } throw new NotSupportedException("Operations other than constructor and method calls are not supported."); }
internal MethodCallInterceptor(Type testType, LiveTest target) : base(testType) { this.target = target; }
/// <summary> /// Returns interceptor for the provided test type, and optionally, instance. /// </summary> /// <param name="testType">Type of the test.</param> /// <param name="test">The test instance.</param> /// <returns>The interceptor.</returns> protected static LiveTest Intercept(Type testType, LiveTest test) { (new SecurityPermission(SecurityPermissionFlag.UnmanagedCode)).Demand(); return((LiveTest)(new MethodCallInterceptor(testType, test)).GetTransparentProxy()); }
/// <summary> /// Invokes the message. /// </summary> /// <param name="msg">The message.</param> /// <returns>The result of invocation.</returns> public override IMessage Invoke(IMessage msg) { IConstructionCallMessage constructorCall = msg as IConstructionCallMessage; if (constructorCall != null) { if (this.target == null) { MethodInfo factoryMethodInfo = Utility.GetInheritedMethod(constructorCall.ActivationType, DynamicConstructionAttribute.InstantiateMethodName, new[] { typeof(Type), typeof(object[]) }); if (factoryMethodInfo == null) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Cannot create an instance of type '{0}' because there is no '{1}' static method defined in its inheritance hierarchy. See '{2}' methods for an example of corresponding method signature.", constructorCall.ActivationType.AssemblyQualifiedName, DynamicConstructionAttribute.InstantiateMethodName, typeof(LiveTest).AssemblyQualifiedName)); } if (!DynamicConstructionAttribute.EnterInstantiationPhase()) { throw new InvalidOperationException("Error in instantiation workflow. Object is already in the middle of instantiation when another instance of the object is created."); } try { this.target = (LiveTest)factoryMethodInfo.Invoke(null, new object[] { constructorCall.ActivationType, constructorCall.Args }); } finally { DynamicConstructionAttribute.LeaveInstantiationPhase(); } } return EnterpriseServicesHelper.CreateConstructionReturnMessage(constructorCall, (MarshalByRefObject)this.GetTransparentProxy()); } IMethodCallMessage methodCall = msg as IMethodCallMessage; if (methodCall != null) { methodCall = new MethodCall(methodCall); MethodCallEventArgs eventArgs = typeof(LiveTest).IsAssignableFrom(methodCall.MethodBase.DeclaringType) ? new MethodCallEventArgs(Interlocked.Increment(ref methodCallId), methodCall.MethodBase, methodCall.Args) : null; if (eventArgs != null) { this.target.OnBeforeMethodCall(this.target, eventArgs); } IMessage result = RemotingServices.GetRealProxy(this.target).Invoke(methodCall); if (eventArgs != null) { this.target.OnAfterMethodCall(this.target, eventArgs); } return result; } throw new NotSupportedException("Operations other than constructor and method calls are not supported."); }
/// <summary> /// Returns interceptor for the provided test type, and optionally, instance. /// </summary> /// <param name="testType">Type of the test.</param> /// <param name="test">The test instance.</param> /// <returns>The interceptor.</returns> protected static LiveTest Intercept(Type testType, LiveTest test) { (new SecurityPermission(SecurityPermissionFlag.UnmanagedCode)).Demand(); return (LiveTest)(new MethodCallInterceptor(testType, test)).GetTransparentProxy(); }