コード例 #1
0
        public void Cannot_mixin_several_delegate_types_with_same_signature()
        {
            var options = new ProxyGenerationOptions();

            options.AddDelegateTypeMixin(typeof(Func <Exception, bool>));
            options.AddDelegateTypeMixin(typeof(Predicate <Exception>));
            Assert.Throws <InvalidMixinConfigurationException>(() => options.Initialize());
        }
コード例 #2
0
ファイル: CastleProxyFactory.cs プロジェクト: ysteiger/moq4
        /// <inheritdoc />
        public override object CreateProxy(Type mockType, Moq.IInterceptor interceptor, Type[] interfaces, object[] arguments)
        {
            // All generated proxies need to implement `IProxy`:
            var additionalInterfaces = new Type[1 + interfaces.Length];

            additionalInterfaces[0] = typeof(IProxy);
            Array.Copy(interfaces, 0, additionalInterfaces, 1, interfaces.Length);

            if (mockType.IsInterface)
            {
                // While `CreateClassProxy` could also be used for interface types,
                // `CreateInterfaceProxyWithoutTarget` is much faster (about twice as fast):
                return(generator.CreateInterfaceProxyWithoutTarget(mockType, additionalInterfaces, this.generationOptions, new Interceptor(interceptor)));
            }
            else if (mockType.IsDelegateType())
            {
                var options = new ProxyGenerationOptions();
                options.AddDelegateTypeMixin(mockType);
                var container = generator.CreateClassProxy(typeof(object), additionalInterfaces, options, new Interceptor(interceptor));
                return(Delegate.CreateDelegate(mockType, container, container.GetType().GetMethod("Invoke")));
            }

            try
            {
                return(generator.CreateClassProxy(mockType, additionalInterfaces, this.generationOptions, arguments, new Interceptor(interceptor)));
            }
            catch (TypeLoadException e)
            {
                throw new ArgumentException(Resources.TypeNotMockable, e);
            }
            catch (MissingMethodException e)
            {
                throw new ArgumentException(Resources.ConstructorNotFound, e);
            }
        }
コード例 #3
0
        /// <inheritdoc />
        public object CreateStunt(Assembly stuntsAssembly, Type baseType, Type[] implementedInterfaces, object[] constructorArguments)
        {
            var notImplemented = false;

            if (baseType.IsInterface)
            {
                var fixedInterfaces = new Type[implementedInterfaces.Length + 1];
                fixedInterfaces[0] = baseType;
                implementedInterfaces.CopyTo(fixedInterfaces, 1);
                implementedInterfaces = fixedInterfaces;
                baseType       = typeof(object);
                notImplemented = true;
            }

            if (!implementedInterfaces.Contains(typeof(IStunt)))
            {
                var fixedInterfaces = new Type[implementedInterfaces.Length + 1];
                fixedInterfaces[0] = typeof(IStunt);
                implementedInterfaces.CopyTo(fixedInterfaces, 1);
                implementedInterfaces = fixedInterfaces;
            }

            if (baseType.BaseType == typeof(MulticastDelegate))
            {
                var mixinOptions = new ProxyGenerationOptions();
                mixinOptions.AddDelegateTypeMixin(baseType);
                var proxy = CreateProxy(typeof(object), implementedInterfaces, mixinOptions, Array.Empty <object>(), notImplemented);
                return(Delegate.CreateDelegate(baseType, proxy, proxy.GetType().GetMethod("Invoke")));
            }
            else
            {
                return(CreateProxy(baseType, implementedInterfaces, options, constructorArguments, notImplemented));
            }
        }
コード例 #4
0
ファイル: DelegateProxyTestCase.cs プロジェクト: hzhhhbb/Core
        private Type GenerateProxyType <TDelegate>()
        {
            var options = new ProxyGenerationOptions();

            options.AddDelegateTypeMixin(typeof(TDelegate));
            return(generator.ProxyBuilder.CreateClassProxyType(typeof(object), null, options));
        }
コード例 #5
0
        public void ProxyGenerator_CreateInterfaceProxyWithoutTarget_can_create_delegate_proxy_without_target()
        {
            var options = new ProxyGenerationOptions();

            options.AddDelegateTypeMixin(typeof(Action));
            var _     = new Interceptor();
            var proxy = generator.CreateInterfaceProxyWithoutTarget(typeof(IComparable), options, _);
        }
コード例 #6
0
        public void ProxyGenerator_CreateClassProxy_can_create_delegate_proxy_without_target()
        {
            var options = new ProxyGenerationOptions();

            options.AddDelegateTypeMixin(typeof(Action));
            var _     = new Interceptor();
            var proxy = generator.CreateClassProxy(typeof(object), options, _);
        }
コード例 #7
0
        public void Can_mixin_several_different_delegate_types_simultaneously()
        {
            var options = new ProxyGenerationOptions();

            options.AddDelegateTypeMixin(typeof(Action));
            options.AddDelegateTypeMixin(typeof(Action <int>));

            var interceptor = new Interceptor();

            var proxy = generator.CreateClassProxy(typeof(object), options, interceptor);

            var action = ProxyUtil.CreateDelegateToMixin <Action>(proxy);

            Assert.NotNull(action);
            action.Invoke();

            var intAction = ProxyUtil.CreateDelegateToMixin <Action <int> >(proxy);

            Assert.NotNull(action);
            intAction.Invoke(42);
        }
コード例 #8
0
        public void ProxyGenerator_CreateInterfaceProxyWithoutTarget_cannot_proceed_to_delegate_type_mixin()
        {
            var options = new ProxyGenerationOptions();

            options.AddDelegateTypeMixin(typeof(Action));

            var interceptor = new Interceptor(shouldProceed: true);

            var proxy  = generator.CreateInterfaceProxyWithoutTarget(typeof(IComparable), options, interceptor);
            var action = ProxyUtil.CreateDelegateToMixin <Action>(proxy);

            Assert.NotNull(action);

            Assert.Throws <NotImplementedException>(() => action.Invoke());
        }
コード例 #9
0
        public void ProxyGenerator_CreateClassProxy_can_create_callable_delegate_proxy_without_target()
        {
            var options = new ProxyGenerationOptions();

            options.AddDelegateTypeMixin(typeof(Action));

            var interceptor = new Interceptor();

            var proxy  = generator.CreateClassProxy(typeof(object), options, interceptor);
            var action = ProxyUtil.CreateDelegateToMixin <Action>(proxy);

            Assert.NotNull(action);

            action.Invoke();
            Assert.AreSame(typeof(Action).GetMethod("Invoke"), interceptor.LastInvocation.Method);
        }
コード例 #10
0
        public void ProxyGenerationOptions_AddDelegateTypeMixin_when_given_delegate_type_succeeds()
        {
            var options = new ProxyGenerationOptions();

            options.AddDelegateTypeMixin(typeof(Action));
        }
コード例 #11
0
        public void ProxyGenerationOptions_AddDelegateTypeMixin_when_given_non_delegate_type_throws_ArgumentException()
        {
            var options = new ProxyGenerationOptions();

            Assert.Throws <ArgumentException>(() => options.AddDelegateTypeMixin(typeof(Exception)));
        }
コード例 #12
0
        public void ProxyGenerationOptions_AddDelegateTypeMixin_when_given_null_throws_ArgumentNullException()
        {
            var options = new ProxyGenerationOptions();

            Assert.Throws <ArgumentNullException>(() => options.AddDelegateTypeMixin(null));
        }