public object GenerateProxy(ICallRouter callRouter, Type typeToProxy,
                                    Type[] additionalInterfaces, object[] constructorArguments)
        {
            VerifyClassHasNotBeenPassedAsAnAdditionalInterface(additionalInterfaces);

            var interceptor            = new CastleForwardingInterceptor(new CastleInvocationMapper(), callRouter);
            var proxyGenerationOptions = GetOptionsToMixinCallRouter(callRouter);
            var proxy = CreateProxyUsingCastleProxyGenerator(typeToProxy, additionalInterfaces,
                                                             constructorArguments, interceptor, proxyGenerationOptions);

            interceptor.StartIntercepting();
            return(proxy);
        }
        public object GenerateComponentProxy(ICallRouter callRouter, Type type, GameObject gameObject)
        {
            var interceptor            = new CastleForwardingInterceptor(new CastleInvocationMapper(), callRouter);
            var proxyGenerationOptions = GetOptionsToMixinCallRouter(callRouter);

            // We can't instantiate the proxy using _proxyGenerator.CreateClassProxy
            // because components cannot be created using the 'new' operator. They must be added to a GameObject instead.
            // So instead, we use reflection to generate the proxy type from the ProxyGenerator
            // and then add it to the GameObect ourselves.
            MethodInfo method = _proxyGenerator.GetType().GetMethod("CreateClassProxyType",
                                                                    BindingFlags.Instance |
                                                                    BindingFlags.NonPublic);

            object[] args      = new object[] { type, null, proxyGenerationOptions };
            Type     proxyType = (Type)method.Invoke(_proxyGenerator, args);

            // Add the proxy component type fo the GameObject.
            var proxy = gameObject.AddComponent(proxyType);

            // We still need to call the constructor generated for the proxy type so that the proxy has access
            // to the interceptors and other proxy options.
            // Use reflection to generate the argument list for the proxy class.
            MethodInfo argsMethod = _proxyGenerator.GetType().GetMethod("BuildArgumentListForClassProxy",
                                                                        BindingFlags.Instance |
                                                                        BindingFlags.NonPublic);

            // Get the constructor arguments for the proxy type.
            args = new object[] { proxyGenerationOptions, new IInterceptor[] { interceptor } };
            List <object> arguments = (List <object>)argsMethod.Invoke(_proxyGenerator, args);

            // Now, we need to use reflection to manually find the correct constructor
            // to call for the proxy type and call it.
            ConstructorInfo[] constructors = proxyType.GetConstructors();
            foreach (ConstructorInfo constructor in constructors)
            {
                if (constructor.GetParameters().Length == arguments.Count)
                {
                    constructor.Invoke(proxy, arguments.ToArray());
                }
            }

            interceptor.StartIntercepting();

            return(proxy);
        }