public InvokationLoggingMock()
        {
            var proxyGen = new Castle.DynamicProxy.ProxyGenerator();

            _interceptor = new CallLoggingInterceptor();
            Object       = proxyGen.CreateInterfaceProxyWithoutTarget <T>(_interceptor);
        }
Esempio n. 2
0
        /// <summary>
        /// The real constructor is private because C# won't run the static constructor (which loads Castle.Core)
        /// before trying to at least somewhat interpret this method body, which contains a Castle reference.
        ///
        /// By making sure that all public constructors are indirected to a "real" method body by one step, assembly
        /// loading appears to work.
        /// </summary>
        private Fake(bool autoStub, object dummy)
        {
            var gen = new Castle.DynamicProxy.ProxyGenerator();

            interceptor = new Interceptor(autoStub);

            Object = gen.CreateInterfaceProxyWithoutTarget <TObj>(interceptor);
        }
Esempio n. 3
0
        public static object CreateProxy(SitecoreClassConfig config, ISitecoreService service, Item item, bool inferType)
        {
            object proxy = null;

            Type type = config.Type;

            if (type.IsInterface)
            {
                proxy = _generator.CreateInterfaceProxyWithoutTarget(type, new InterfaceMethodInterceptor(config, item, service));
            }
            else
            {
                proxy = _generator.CreateClassProxy(type, _options, new ProxyClassInterceptor(type,
                                                                                              service,
                                                                                              item, inferType));
            }

            return(proxy);
        }
Esempio n. 4
0
        /// <summary>
        /// Creates an instance of proxy class.
        /// </summary>
        /// <param name="targetType">The type from which the proxy class is inherited.</param>
        /// <returns>The proxy class instance.</returns>
        public object CreateClassProxy(Type targetType)
        {
            Check.NotNull(targetType, nameof(targetType));
            if (targetType.IsValueType)
            {
                throw new ProxyGeneratorException(string.Format("Value type {0} is not supported for proxy generation.", targetType.Name));
            }
            if (targetType == typeof(string))
            {
                throw new ProxyGeneratorException(string.Format("Type String is not supported for proxy generation."));
            }
            if (IsTypeAccessible(targetType))
            {
                throw new TypeAccessiblityException(string.Format("Type {0} is not accessible.", targetType.FullName));
            }
            if (targetType.IsInterface)
            {
                return(generator.CreateInterfaceProxyWithoutTarget(targetType));
            }

            var defaultCtor = GetContructorsForType(targetType).FirstOrDefault(x => x.GetParameters().Count() == 0);

            if (IsConstructorAccessible(defaultCtor))
            {
                try
                {
                    return(generator.CreateClassProxy(targetType));
                }
                catch (Castle.DynamicProxy.ProxyGenerationException x)
                {
                    throw new TypeAccessiblityException("Castle instance creation error", x);
                }
            }

            return(UseAnyAvailableConstructor(targetType));
        }
Esempio n. 5
0
 /// <summary>
 /// Creates the proxy for the target type.
 /// </summary>
 public object Create(Type targetType)
 {
     var proxy =  new Castle.DynamicProxy.ProxyGenerator();
     return proxy.CreateInterfaceProxyWithoutTarget(targetType, new ProxiedMethodInterceptor(interceptor));
 }
        private static IUnityContainer GetDecoratingProxy(IUnityContainer container)
        {
            var proxyGenerator = new Castle.DynamicProxy.ProxyGenerator();

            return(proxyGenerator.CreateInterfaceProxyWithoutTarget <IUnityContainer>(new DecoratingInterceptor(container)));
        }