private object GenerateDelegateProxy(ICallRouter callRouter, Type delegateType, Type[] additionalInterfaces, object[] constructorArguments)
        {
            VerifyNoAdditionalInterfacesGivenForDelegate(additionalInterfaces);
            VerifyNoConstructorArgumentsGivenForDelegate(constructorArguments);

            var forwardingInterceptor = CreateForwardingInterceptor(callRouter);
            // Keep this interceptor, so that real proxy ID can be retrieved by proxy.Target.ToString().
            var proxyIdInterceptor = new ProxyIdInterceptor(delegateType);

            var proxyGenerationOptions = GetOptionsToMixinCallRouterProvider(callRouter);

            proxyGenerationOptions.AddDelegateTypeMixin(delegateType);

            var proxy = CreateProxyUsingCastleProxyGenerator(
                typeToProxy: typeof(object),
                additionalInterfaces: null,
                constructorArguments: null,
                interceptors: new IInterceptor[] { proxyIdInterceptor, forwardingInterceptor },
                proxyGenerationOptions);

            forwardingInterceptor.SwitchToFullDispatchMode();

            // Ideally we should use ProxyUtil.CreateDelegateToMixin(proxy, delegateType).
            // But it's slower than code below due to extra checks it performs.
            return(proxy.GetType().GetInvokeMethod().CreateDelegate(delegateType, proxy));
        }
            public override bool ShouldInterceptMethod(Type type, MethodInfo methodInfo)
            {
                // Always intercept object.ToString() as we would like to return proxy id as a result.
                if (ProxyIdInterceptor.IsDefaultToStringMethod(methodInfo))
                {
                    return(true);
                }

                return(IsNotCallRouterProviderMethod(methodInfo) &&
                       IsNotBaseObjectMethod(methodInfo) &&
                       base.ShouldInterceptMethod(type, methodInfo));
            }
        private object GenerateTypeProxy(ICallRouter callRouter, Type typeToProxy, Type[] additionalInterfaces, object[] constructorArguments)
        {
            VerifyClassHasNotBeenPassedAsAnAdditionalInterface(additionalInterfaces);

            var proxyIdInterceptor    = new ProxyIdInterceptor(typeToProxy);
            var forwardingInterceptor = CreateForwardingInterceptor(callRouter);

            var proxyGenerationOptions = GetOptionsToMixinCallRouterProvider(callRouter);

            var proxy = CreateProxyUsingCastleProxyGenerator(
                typeToProxy,
                additionalInterfaces,
                constructorArguments,
                new IInterceptor[] { proxyIdInterceptor, forwardingInterceptor },
                proxyGenerationOptions);

            forwardingInterceptor.SwitchToFullDispatchMode();
            return(proxy);
        }
Example #4
0
        public object GenerateProxy(ICallRouter callRouter, Type typeToProxy, Type[] additionalInterfaces, object[] constructorArguments)
        {
            VerifyClassHasNotBeenPassedAsAnAdditionalInterface(additionalInterfaces);

            var proxyIdInterceptor    = new ProxyIdInterceptor(typeToProxy);
            var forwardingInterceptor = new CastleForwardingInterceptor(
                new CastleInvocationMapper(
                    new CallFactory(),
                    _argSpecificationDequeue),
                callRouter);

            var proxyGenerationOptions = GetOptionsToMixinCallRouter(callRouter);
            var proxy = CreateProxyUsingCastleProxyGenerator(
                typeToProxy,
                additionalInterfaces,
                constructorArguments,
                new IInterceptor[] { proxyIdInterceptor, forwardingInterceptor },
                proxyGenerationOptions);

            forwardingInterceptor.SwitchToFullDispatchMode();
            return(proxy);
        }