コード例 #1
0
        /// <inheritdoc cref="ITypeDecoratorEmitter" />
        public void ImplementDecorator(Type signature)
        {
            Ensures.NotNull(signature);
            Ensures.IsInterface(signature);

            var decorateeField   = Dependencies.CreateDecorateeDependency(Type, signature);
            var interceptorField = Dependencies.CreateInterceptorDependency(Type);

            Dependencies.CreateConstructor(Type, decorateeField, interceptorField);
            Type.AddInterfaceImplementation(signature);

            foreach (var property in signature.GetProperties())
            {
                var emitter = PropertyEmitterFactory.CreatePropertyEmitterFor(
                    property, Type, decorateeField, interceptorField);
                emitter.EmitPropertyImplementation();
            }

            foreach (var method in signature.GetMethods().Where(m => !m.IsSpecialName))
            {
                var emitter = MethodEmitterFactory.CreateMethodDecoratorEmitterFor(
                    method, Type, decorateeField, interceptorField);
                emitter.EmitMethodImplementation();
            }
        }
コード例 #2
0
        /// <inheritdoc cref="IMockedDependencyFactory" />
        public IMockedDependency <T> CreateMockedDependency <T>(MockBehavior behavior)
            where T : notnull
        {
            Ensures.IsInterface <T>();

            var arrangements = new ArrangementCollection();
            var interceptor  = InterceptorFactory.CreateInterceptorFor(behavior, arrangements);
            var proxy        = DynamicProxyFactory.CreateForInterface <T>(interceptor);

            return(new MockedDependency <T>(arrangements, interceptor, proxy));
        }
コード例 #3
0
        /// <inheritdoc cref="IMockedDependencyFactory" />
        public IMockedDependency <T> CreateDecoratedDependency <T>(T decoratee)
            where T : notnull
        {
            Ensures.IsInterface <T>();

            var arrangements = new ArrangementCollection();
            var interceptor  = InterceptorFactory.CreateInterceptorFor(MockBehavior.Partial, arrangements);
            var proxy        = DynamicProxyFactory.CreateDecorator <T>(decoratee, interceptor);

            return(new MockedDependency <T>(arrangements, interceptor, proxy));
        }
コード例 #4
0
        /// <inheritdoc cref="IMockedDependencyFactory" />
        public IMockedDependency CreateMockedDependency(Type dependency, MockBehavior behavior)
        {
            Ensures.NotNull(dependency, nameof(dependency));
            Ensures.IsInterface(dependency);

            var arrangements         = new ArrangementCollection();
            var interceptor          = InterceptorFactory.CreateInterceptorFor(behavior, arrangements);
            var proxy                = DynamicProxyFactory.CreateForInterface(dependency, interceptor);
            var mockedDependencyType = typeof(MockedDependency <>).MakeGenericType(dependency);
            var instance             = Activator.CreateInstance(mockedDependencyType, new[] { arrangements, interceptor, proxy });

            if (instance is IMockedDependency mockedDependency)
            {
                return(mockedDependency);
            }

            throw new InvalidOperationException("Invalid mocked dependency instance");
        }