protected override object VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProviderEngineScope scope)
        {
            object[] parameterValues = new object[constructorCallSite.ParameterCallSites.Length];
            for (var index = 0; index < parameterValues.Length; index++)
            {
                parameterValues[index] = VisitCallSite(constructorCallSite.ParameterCallSites[index], scope);
            }

            try
            {
                var obj           = constructorCallSite.ConstructorInfo.Invoke(parameterValues);
                var interceptors  = InterceptorRuntimeCreate.CreatedInterceptors;
                var implementName = constructorCallSite.ImplementationType.FullName;
                if (interceptors != null && interceptors.Count > 0 &&
                    InterceptorRuntimeCreate.CanIntercept(implementName))
                {
                    Castle.DynamicProxy.ProxyGenerator generator = new Castle.DynamicProxy.ProxyGenerator();

                    if (constructorCallSite.ServiceType.IsInterface)
                    {
                        try
                        {
                            obj = generator.CreateInterfaceProxyWithTarget(constructorCallSite.ServiceType,
                                                                           obj,
                                                                           interceptors.ToArray());
                        }
                        catch (Exception)
                        {
                            // ignored
                        }
                    }
                    else if (constructorCallSite.ServiceType.IsClass &&
                             !constructorCallSite.ServiceType.IsAbstract &&
                             !constructorCallSite.ServiceType.IsSealed)
                    {
                        obj = generator.CreateClassProxyWithTarget(constructorCallSite.ServiceType, obj,
                                                                   parameterValues, interceptors.ToArray());
                    }
                }
                return(obj);
            }
            catch (Exception ex) when(ex.InnerException != null)
            {
                ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
                // The above line will always throw, but the compiler requires we throw explicitly.
                throw;
            }
        }
    private static void Main(string[] args)
    {
        var msg = Message.CreateMessage(MessageVersion.Soap11, "*");

        msg.Headers.Clear();
        var proxyGenerator = new Castle.DynamicProxy.ProxyGenerator();
        var proxiedMessage = proxyGenerator.CreateClassProxyWithTarget(msg, new ProxyGenerationOptions(),
                                                                       new ToStringInterceptor());
        var initialResult = msg.ToString();
        var proxiedResult = proxiedMessage.ToString();

        Console.WriteLine("Initial result");
        Console.WriteLine(initialResult);
        Console.WriteLine();
        Console.WriteLine("Proxied result");
        Console.WriteLine(proxiedResult);
        Console.ReadLine();
    }
예제 #3
0
        /// <summary>
        /// Enables logging of specific mock instance
        /// </summary>
        /// <param name="methodsToIgnore">Specifies method or property names to ignore when logging
        /// (e.g. utility functions about which we don't care)</param>
        public static Mock <T> Logged <T>(this Mock <T> mock, params string[] methodsToIgnore) where T : class
        {
            object mockedObject = mock.Object;
            object proxy;

            if (proxygenerator == null)
            {
                proxygenerator = new Castle.DynamicProxy.ProxyGenerator();
            }

            if (typeof(T).IsInterface)
            {
                proxy = proxygenerator.CreateInterfaceProxyWithTarget(typeof(T), mockedObject, new LoggedMockInterceptor(methodsToIgnore));
            }
            else
            {
                proxy = proxygenerator.CreateClassProxyWithTarget(typeof(T), mockedObject, new LoggedMockInterceptor(methodsToIgnore));
            }

            var field = mock.GetType().GetField("instance", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

            field.SetValue(mock, proxy);
            return(mock);
        }