public T CreateProxy <T>(ICallInterceptor interceptor, Type[] interfaces, object[] arguments)
        {
            var mockType = typeof(T);

            if (mockType.IsInterface())
            {
                return((T)generator.CreateInterfaceProxyWithoutTarget(mockType, interfaces, new Interceptor(interceptor)));
            }

            try
            {
                return((T)generator.CreateClassProxy(mockType, interfaces, new ProxyGenerationOptions(), arguments, new Interceptor(interceptor)));
            }
            catch (TypeLoadException e)
            {
                throw new ArgumentException(Resources.InvalidMockClass, e);
            }
#if !NETFX_CORE
            catch (MissingMethodException e)
#else
            catch (SpecialMissingMethodException e)
#endif
            {
                throw new ArgumentException(Resources.ConstructorNotFound, e);
            }
        }
Exemple #2
0
        public static T CreateDynamicProxy <T>(ICallInterceptor interceptor, object context)
        {
            Type proxyType = InterfaceTypeToProxyType[typeof(T)];
            var  proxy     = (T)Activator.CreateInstance(proxyType);

            proxy.GetType().GetField("Interceptor").SetValue(proxy, interceptor);
            proxy.GetType().GetField("Context").SetValue(proxy, context);
            return(proxy);
        }
        private ICallHandler GetOrCreateHandler(ICallInterceptor callInterceptor)
        {
            ICallHandler result;

            if (!cacheHandler.TryGetValue(callInterceptor, out result))
            {
                result = new InterceptedCallHandler(callInterceptor);
                cacheHandler.Add(callInterceptor, result);
            }

            return(result);
        }
        /// <summary>
        /// Constructs a new InterceptingAdapterCallHandler object, given an interceptor
        /// and an adapted object.
        /// </summary>
        /// <remarks>
        /// <para>
        /// When creating a new InterceptingAdapterCallHandler, caller can specify only
        /// one adapter, one interceptor, or both of them.
        /// </para>
        /// <para>
        /// If the adapted object is null, the HandleCall method will only pass the method
        /// call information to the interceptor, and ignore the adapted object.
        /// </para>
        /// <para>
        /// If the interceptor is null, the HandleCall method will not intercept the call,
        /// and dispatches the call directly to the adapted object.
        /// </para>
        /// </remarks>
        /// <param name="adapted">
        /// Specifies the target object to be wrapped and recieve
        /// method calls, or null if there is no need to wrap an object.
        /// </param>
        /// <param name="interceptor">
        /// Specifies the interceptor implementation to be notified before and after
        /// each method call.
        /// </param>
        public InterceptingAdapterEmittedTypeHanlder(object adapted, ICallInterceptor interceptor = null)
        {
            if ((adapted == null) && (interceptor == null))
            {
                throw new ArgumentException(
                          "Either adapted or interceptor object should be specified for constructing a InterceptingAdapterEmittedTypeHanlder object.");
            }

            _adapted     = adapted;
            _interceptor = interceptor;

            if (adapted != null)
            {
                _adaptedType = adapted.GetType();
            }
        }
Exemple #5
0
        public void RegisterType()
        {
            ContainerBuilder builder = new ContainerBuilder();

            builder.RegisterType <CallLogger>()
            .As <ILogger>()
            .As <ICallInterceptor>();
            IContainer container = builder.Build();

            using (ILifetimeScope scope = container.BeginLifetimeScope())
            {
                ILogger          iLogger          = scope.Resolve <ILogger>();
                ICallInterceptor iCallInterceptor = scope.Resolve <ICallInterceptor>();
                TestUtilities.WriteLine(iLogger);
                TestUtilities.WriteLine(iCallInterceptor);
                TestUtilities.Attach();
            }
        }
Exemple #6
0
        /// <summary>
        /// http://autofac.readthedocs.io/en/latest/register/registration.html#services-vs-components
        /// </summary>
        private static void ComponentWithAnyNumberOfServices()
        {
            Console.WriteLine("\nComponentWithAnyNumberOfServices:\n");
            ContainerBuilder builder = new ContainerBuilder();

            builder.RegisterType <CallLogger>()
            .As <ILogger>()
            .As <ICallInterceptor>();
            IContainer container = builder.Build();

            using (ILifetimeScope lifetimeScope = container.BeginLifetimeScope())
            {
                ILogger logger = lifetimeScope.Resolve <ILogger>();
                Console.WriteLine("ILogger resolved");
                ICallInterceptor icaCallInterceptor = lifetimeScope.Resolve <ICallInterceptor>();
                Console.WriteLine("ICallInterceptor resolved");
            }
        }
Exemple #7
0
		/// <inheritdoc />
		public object CreateProxy(Type mockType, ICallInterceptor interceptor, Type[] interfaces, object[] arguments)
		{
			if (mockType.IsInterface) {
				return generator.CreateInterfaceProxyWithoutTarget(mockType, interfaces, proxyOptions, new Interceptor(interceptor));
			}

			try
			{
				return generator.CreateClassProxy(mockType, interfaces, proxyOptions, arguments, new Interceptor(interceptor));
			}
			catch (TypeLoadException e)
			{
				throw new ArgumentException(Resources.InvalidMockClass, e);
			}
			catch (MissingMethodException e)
			{
				throw new ArgumentException(Resources.ConstructorNotFound, e);
			}
		}
Exemple #8
0
        /// <inheritdoc />
        public object CreateProxy(Type mockType, ICallInterceptor interceptor, Type[] interfaces, object[] arguments)
        {
            if (mockType.IsInterface)
            {
                return(generator.CreateInterfaceProxyWithoutTarget(mockType, interfaces, proxyOptions, new Interceptor(interceptor)));
            }

            try
            {
                return(generator.CreateClassProxy(mockType, interfaces, proxyOptions, arguments, new Interceptor(interceptor)));
            }
            catch (TypeLoadException e)
            {
                throw new ArgumentException(Resources.InvalidMockClass, e);
            }
            catch (MissingMethodException e)
            {
                throw new ArgumentException(Resources.ConstructorNotFound, e);
            }
        }
Exemple #9
0
        /// <summary>
        /// http://autofac.readthedocs.io/en/latest/register/registration.html#services-vs-components
        /// If you want to expose a component as a set of services as well as
        /// using the default service, use the AsSelf method:
        /// </summary>
        private static void AsSelfSample()
        {
            Console.WriteLine("\nAsSelfSample:\n");
            ContainerBuilder builder = new ContainerBuilder();

            builder.RegisterType <CallLogger>()
            .AsSelf()
            .As <ILogger>()
            .As <ICallInterceptor>();
            IContainer container = builder.Build();

            using (ILifetimeScope lifetimeScope = container.BeginLifetimeScope())
            {
                ILogger logger = lifetimeScope.Resolve <ILogger>();
                Console.WriteLine("ILogger resolved");
                ICallInterceptor icaCallInterceptor = lifetimeScope.Resolve <ICallInterceptor>();
                Console.WriteLine("ICallInterceptor resolved");
                CallLogger callLogger = lifetimeScope.Resolve <CallLogger>();
                Console.WriteLine("CallLogger resolved");
            }
        }
Exemple #10
0
        public void AsSelf()
        {
            ContainerBuilder builder = new ContainerBuilder();

            builder.RegisterType <CallLogger>()
            .AsSelf()
            .As <ILogger>()
            .As <ICallInterceptor>();
            IContainer container = builder.Build();

            using (ILifetimeScope scope = container.BeginLifetimeScope())
            {
                // These will all work because we exposed
                // the appropriate services in the registration:
                ILogger          iLogger         = scope.Resolve <ILogger>();
                ICallInterceptor callInterceptor = scope.Resolve <ICallInterceptor>();
                CallLogger       callLogger      = scope.Resolve <CallLogger>();
                TestUtilities.WriteLine(iLogger);
                TestUtilities.WriteLine(callInterceptor);
                TestUtilities.WriteLine(callLogger);
                TestUtilities.Attach();
            }
        }
Exemple #11
0
        /// <inheritdoc />
        public object CreateProxy(Type mockType, ICallInterceptor interceptor, Type[] interfaces, object[] arguments)
        {
            if (mockType.IsInterface)
            {
                // Add type to additional interfaces and mock System.Object instead.
                // This way it is also possible to mock System.Object methods.
                Array.Resize(ref interfaces, interfaces.Length + 1);
                interfaces[interfaces.Length - 1] = mockType;
                mockType = typeof(object);
            }

            try
            {
                return(generator.CreateClassProxy(mockType, interfaces, proxyOptions, arguments, new Interceptor(interceptor)));
            }
            catch (TypeLoadException e)
            {
                throw new ArgumentException(Resources.InvalidMockClass, e);
            }
            catch (MissingMethodException e)
            {
                throw new ArgumentException(Resources.ConstructorNotFound, e);
            }
        }
Exemple #12
0
		/// <inheritdoc />
		public object CreateProxy(Type mockType, ICallInterceptor interceptor, Type[] interfaces, object[] arguments)
		{
			if (mockType.IsInterface)
			{
				// Add type to additional interfaces and mock System.Object instead.
				// This way it is also possible to mock System.Object methods.
				Array.Resize(ref interfaces, interfaces.Length + 1);
				interfaces[interfaces.Length - 1] = mockType;
				mockType = typeof(object);
			}

			try
			{
				return generator.CreateClassProxy(mockType, interfaces, proxyOptions, arguments, new Interceptor(interceptor));
			}
			catch (TypeLoadException e)
			{
				throw new ArgumentException(Resources.InvalidMockClass, e);
			}
			catch (MissingMethodException e)
			{
				throw new ArgumentException(Resources.ConstructorNotFound, e);
			}
		}
Exemple #13
0
 internal Interceptor(ICallInterceptor interceptor)
 {
     this.interceptor = interceptor;
 }
Exemple #14
0
			internal Interceptor(ICallInterceptor interceptor)
			{
				this.interceptor = interceptor;
			}
        private ICallHandler GetOrCreateHandler(ICallInterceptor callInterceptor)
        {
            ICallHandler result;

            if (!cacheHandler.TryGetValue(callInterceptor, out result))
            {
                result = new InterceptedCallHandler(callInterceptor);
                cacheHandler.Add(callInterceptor, result);
            }

            return result;
        }
 /// <summary>
 /// Constructs a new InterceptingAdapterCallHandler without any adapted
 /// object, with the specified interceptor.
 /// </summary>
 /// <param name="interceptor">
 /// Specifies the interceptor implementation to be notified before and after
 /// each method call.
 /// </param>
 public InterceptingAdapterEmittedTypeHanlder(ICallInterceptor interceptor)
     : this(null, interceptor)
 {
 }