Esempio n. 1
0
    public object Intercept(MethodBase decoratedMethod, MethodBase implementationMethod, object[] arguments)
    {
        if (this.implementationMethodTarget == null)
        {
            throw new InvalidOperationException("Something has gone seriously wrong with StaticProxy.Fody." +
                                                ".Initialize(implementationMethodTarget) must be called once before any .Intercept(..)");
        }

        // since we only support methods, not constructors, this is actually a MethodInfo
        var decoratedMethodInfo = (MethodInfo)decoratedMethod;

        ITargetInvocation targetInvocation = this.targetInvocationFactory.Create(this.implementationMethodTarget, implementationMethod);

        IInvocation invocation = this.invocationFactory
                                 .Create(targetInvocation, decoratedMethodInfo, arguments, this.interceptors.ToArray());

        invocation.Proceed();

        if (invocation.ReturnValue == null && !this.typeInformation.IsNullable(decoratedMethodInfo.ReturnType))
        {
            string message = string.Format(
                CultureInfo.InvariantCulture,
                "Method {0}.{1} has return type {2} which is a value type. After the invocation the invocation the return value was null. Please ensure that your interceptors call IInvocation.Proceed() or sets a valid IInvocation.ReturnValue.",
                this.implementationMethodTarget.GetType().FullName,
                decoratedMethodInfo,
                decoratedMethodInfo.ReturnType.FullName);
            throw new InvalidOperationException(message);
        }

        return(invocation.ReturnValue);
    }
Esempio n. 2
0
 public IInvocation Create(
     ITargetInvocation targetInvocation,
     MethodInfo decoratedMethod,
     object[] arguments,
     IDynamicInterceptor[] interceptors)
 {
     return(new Invocation(targetInvocation, decoratedMethod, arguments, interceptors));
 }
 public IInvocation Create(
     ITargetInvocation targetInvocation,
     MethodInfo decoratedMethod,
     object[] arguments,
     IDynamicInterceptor[] interceptors)
 {
     return new Invocation(targetInvocation, decoratedMethod, arguments, interceptors);
 }
Esempio n. 4
0
        public void Create_WhenImplementationMethodNotIsNull_MustReturnWithTargetInvocation()
        {
            var target = new Mock <IFakeTarget>();

            ITargetInvocation targetInvocation = this.testee.Create(target.Object, typeof(IFakeTarget).GetMethod("Foo"));

            targetInvocation.Should().BeOfType <WithTargetInvocation>();

            targetInvocation.InvokeMethodOnTarget(new object[0]);
            target.Verify(x => x.Foo());
        }
Esempio n. 5
0
        public Invocation(ITargetInvocation targetInvocation, MethodInfo decoratedMethod, object[] arguments, IDynamicInterceptor[] interceptors)
        {
            this.targetInvocation = targetInvocation;
            this.decoratedMethod  = decoratedMethod;
            this.arguments        = arguments;
            this.interceptors     = interceptors;

            this.parameterTypes = new Lazy <Type[]>(() => this.decoratedMethod.GetParameters().Select(x => x.ParameterType).ToArray());

            this.genericArguments = new Lazy <Type[]>(() => this.decoratedMethod.GetGenericArguments());
        }
Esempio n. 6
0
        public Invocation(ITargetInvocation targetInvocation, MethodInfo decoratedMethod, object[] arguments, IDynamicInterceptor[] interceptors)
        {
            this.targetInvocation = targetInvocation;
            this.decoratedMethod = decoratedMethod;
            this.arguments = arguments;
            this.interceptors = interceptors;

            this.parameterTypes = new Lazy<Type[]>(() => this.decoratedMethod.GetParameters().Select(x => x.ParameterType).ToArray());

            this.genericArguments = new Lazy<Type[]>(() => this.decoratedMethod.GetGenericArguments());
        }
Esempio n. 7
0
        public void Create_WhenImplementationMethodIsNull_MustReturnWithoutTargetInvocation()
        {
            ITargetInvocation targetInvocation = this.testee.Create(null, null);

            targetInvocation.Should().BeOfType <WithoutTargetInvocation>();
        }