public MethodExecutionExpression(MutableMethodInfo method)
 {
     _method = method;
     _body   = Call(
         new ThisExpression(method.DeclaringType),
         NonVirtualCallMethodInfoAdapter.Adapt(method.UnderlyingSystemMethodInfo),
         method.ParameterExpressions.Cast <Expression>());
 }
        public void Adapt_Method()
        {
            var method = ReflectionObjectMother.GetSomeMethod();

            var adapter = NonVirtualCallMethodInfoAdapter.Adapt(method);

            Assert.That(adapter.AdaptedMethod, Is.SameAs(method));
        }
Exemplo n.º 3
0
        public static MethodExecutionExpression Adapt(MutableMethodInfo method)
        {
            var instance   = new ThisExpression(method.DeclaringType);
            var baseMethod = NonVirtualCallMethodInfoAdapter.Adapt(method.UnderlyingSystemMethodInfo);
            var parameters = method.ParameterExpressions.Cast <Expression>();
            var body       = Call(instance, baseMethod, parameters);

            return(new MethodExecutionExpression(method, body));
        }
        public void Adapt_Constructor()
        {
            var ctor = ReflectionObjectMother.GetSomeConstructor();

            var adapter = NonVirtualCallMethodInfoAdapter.Adapt(ctor);

            Assert.That(adapter.AdaptedMethod, Is.TypeOf <ConstructorAsMethodInfoAdapter>());
            var constructorAsMethodInfoAdapter = (ConstructorAsMethodInfoAdapter)adapter.AdaptedMethod;

            Assert.That(constructorAsMethodInfoAdapter.AdaptedConstructor, Is.SameAs(ctor));
        }
        public void MakeSerializable_SerializableInterfaceType_SerializedFields()
        {
            var       dummyField    = _serializableInterfaceProxy.AddField("input field", FieldAttributes.Private, typeof(int));
            var       fakeFieldType = ReflectionObjectMother.GetSomeType();
            FieldInfo fakeField     = MutableFieldInfoObjectMother.Create(_serializableInterfaceProxy, type: fakeFieldType);
            var       fakeMapping   = new[] { Tuple.Create("fake key", fakeField) };

            _serializableFieldFinderMock
            .Setup(mock => mock.GetSerializableFieldMapping(It.Is <IEnumerable <FieldInfo> > (fields => fields.SequenceEqual(new[] { dummyField }))))
            .Returns(fakeMapping)
            .Verifiable();

            _enabler.MakeSerializable(_serializableInterfaceProxy, _someInitializationMethod);

            _serializableFieldFinderMock.Verify();
            Assert.That(_serializableInterfaceProxy.AddedInterfaces, Is.Empty);
            Assert.That(_serializableInterfaceProxy.AddedMethods, Has.Count.EqualTo(1));

            var baseMethod =
                NormalizingMemberInfoFromExpressionUtility.GetMethod((SerializableInterfaceType obj) => obj.GetObjectData(null, new StreamingContext()));
            var method             = _serializableInterfaceProxy.AddedMethods.Single();
            var expectedMethodBody = Expression.Block(
                typeof(void),
                Expression.Call(
                    new ThisExpression(_serializableInterfaceProxy),
                    NonVirtualCallMethodInfoAdapter.Adapt(baseMethod),
                    method.ParameterExpressions.Cast <Expression>()),
                Expression.Call(
                    method.ParameterExpressions[0],
                    "AddValue",
                    Type.EmptyTypes,
                    Expression.Constant("fake key"),
                    Expression.Field(new ThisExpression(_serializableInterfaceProxy), fakeField)));

            ExpressionTreeComparer.CheckAreEqualTrees(expectedMethodBody, method.Body);

            var baseCtor         = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new SerializableInterfaceType(null, new StreamingContext()));
            var ctor             = _serializableInterfaceProxy.AddedConstructors.Single();
            var getValueMethod   = NormalizingMemberInfoFromExpressionUtility.GetMethod((SerializationInfo obj) => obj.GetValue("", null));
            var expectedCtorBody = Expression.Block(
                typeof(void),
                Expression.Call(
                    new ThisExpression(_serializableInterfaceProxy),
                    NonVirtualCallMethodInfoAdapter.Adapt(baseCtor),
                    ctor.ParameterExpressions.Cast <Expression>()),
                Expression.Assign(
                    Expression.Field(new ThisExpression(_serializableInterfaceProxy), fakeField),
                    Expression.Convert(
                        Expression.Call(ctor.ParameterExpressions[0], getValueMethod, Expression.Constant("fake key"), Expression.Constant(fakeFieldType)),
                        fakeFieldType)));

            ExpressionTreeComparer.CheckAreEqualTrees(expectedCtorBody, ctor.Body);
        }
Exemplo n.º 6
0
        private MethodInfo CreateNonVirtualCallTrampoline(CodeGenerationContext context, MethodInfo method, string trampolineName)
        {
            var methodDeclaration = MethodDeclaration.CreateEquivalent(method);
            var trampoline        = context.MutableType.AddMethod(
                trampolineName,
                MethodAttributes.Private,
                methodDeclaration,
                ctx => Expression.Call(ctx.This, NonVirtualCallMethodInfoAdapter.Adapt(method), ctx.Parameters.Cast <Expression>()));

            _memberEmitter.AddMethod(context, trampoline);

            return(trampoline);
        }
Exemplo n.º 7
0
        public MethodCallExpression CallBase(MethodInfo baseMethod, IEnumerable <Expression> arguments)
        {
            ArgumentUtility.CheckNotNull("baseMethod", baseMethod);
            ArgumentUtility.CheckNotNull("arguments", arguments);

            EnsureNotStatic();
            CheckNotStatic(baseMethod);
            CheckNotAbstract(baseMethod);
            CheckNoGenericMethodDefinition(baseMethod);
            CheckVisibility(baseMethod);
            // TODO: Check if really base call!

            return(Expression.Call(This, NonVirtualCallMethodInfoAdapter.Adapt(baseMethod), arguments));
        }
Exemplo n.º 8
0
        public void Emit_MethodInfo_BaseCallMethodInfo_TurnsCallvirt_IntoCall()
        {
            var method = ReflectionObjectMother.GetSomeMethod();
            var fakeEmittableOperand = MockRepository.GenerateStub <MethodInfo> ();

            _emittableOperandProviderStub.Stub(stub => stub.GetEmittableMethod(method)).Return(fakeEmittableOperand);

            _innerILGeneratorMock.Expect(mock => mock.Emit(OpCodes.Call, fakeEmittableOperand));

            var baseAdaptedMethod = new NonVirtualCallMethodInfoAdapter(method);

            _decorator.Emit(OpCodes.Callvirt, baseAdaptedMethod);

            _innerILGeneratorMock.VerifyAllExpectations();
        }
        public void Emit_MethodInfo_BaseCallMethodInfo_TurnsCallvirt_IntoCall()
        {
            var method = ReflectionObjectMother.GetSomeMethod();
            var fakeEmittableOperand = new Mock <MethodInfo>().Object;

            _emittableOperandProviderStub.Setup(stub => stub.GetEmittableMethod(method)).Returns(fakeEmittableOperand);

            _innerILGeneratorMock.Setup(mock => mock.Emit(OpCodes.Call, fakeEmittableOperand)).Verifiable();

            var baseAdaptedMethod = new NonVirtualCallMethodInfoAdapter(method);

            _decorator.Emit(OpCodes.Callvirt, baseAdaptedMethod);

            _innerILGeneratorMock.Verify();
        }
Exemplo n.º 10
0
        public void EmitCall_MethodInfo_BaseCallMethodInfo()
        {
            var method = ReflectionObjectMother.GetSomeMethod();
            var optionalParameterTypes = new[] { ReflectionObjectMother.GetSomeType() };
            var fakeEmittableOperand   = MockRepository.GenerateStub <MethodInfo> ();

            _emittableOperandProviderStub.Stub(stub => stub.GetEmittableMethod(method)).Return(fakeEmittableOperand);

            _innerILGeneratorMock.Expect(mock => mock.EmitCall(OpCodes.Call, fakeEmittableOperand, optionalParameterTypes));

            var baseAdaptedMethod = new NonVirtualCallMethodInfoAdapter(method);

            _decorator.EmitCall(OpCodes.Call, baseAdaptedMethod, optionalParameterTypes);

            _innerILGeneratorMock.VerifyAllExpectations();
        }
        public void Intercept_AddsOverride_AndWrapsBaseCallInTryFinally()
        {
            _interceptor.Intercept(_proxyType);

            Assert.That(_proxyType.AddedMethods, Has.Count.EqualTo(1));
            var method = _proxyType.AddedMethods.Single();

            var expectedBody =
                Expression.Block(
                    Expression.Call(typeof(CurrentPropertyManager), "PreparePropertyAccess", null, Expression.Constant(_propertyName)),
                    Expression.TryFinally(
                        Expression.Call(
                            new ThisExpression(_proxyType),
                            NonVirtualCallMethodInfoAdapter.Adapt(_interceptedMethod),
                            Expression.Parameter(typeof(object), "obj")),
                        Expression.Call(typeof(CurrentPropertyManager), "PropertyAccessFinished", null)));

            ExpressionTreeComparer.CheckAreEqualTrees(expectedBody, method.Body);
        }
Exemplo n.º 12
0
        public void CreateProxy_CopiesAccessibleInstanceConstructors_WithPublicVisibility()
        {
            var result = _factory.CreateProxy(_domainType, ProxyKind.AssembledType).Type;

            Assert.That(result.AddedConstructors, Has.Count.EqualTo(1));
            var ctor = result.AddedConstructors.Single();

            Assert.That(ctor.IsStatic, Is.False);
            Assert.That(ctor.IsPublic, Is.True, "Changed from 'family or assembly'.");
            Assert.That(ctor.IsHideBySig, Is.True);

            var parameter = ctor.GetParameters().Single();

            CustomParameterInfoTest.CheckParameter(parameter, ctor, 0, "i", typeof(int).MakeByRefType(), ParameterAttributes.Out);

            var baseCtor     = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new DomainType(out Dev <int> .Dummy));
            var expectedBody = Expression.Call(
                new ThisExpression(result), NonVirtualCallMethodInfoAdapter.Adapt(baseCtor), ctor.ParameterExpressions.Cast <Expression>());

            ExpressionTreeComparer.CheckAreEqualTrees(expectedBody, ctor.Body);
        }
 private MethodCallExpression CallConstructor(ConstructorInfo constructor, ICollection <Expression> arguments)
 {
     return(Expression.Call(This, NonVirtualCallMethodInfoAdapter.Adapt(constructor), arguments));
 }
Exemplo n.º 14
0
        private void CheckConstructorCall(ConstructorInfo expectedConstructor, Expression[] arguments, MethodCallExpression actualCall)
        {
            var expected = Expression.Call(new ThisExpression(_declaringType), NonVirtualCallMethodInfoAdapter.Adapt(expectedConstructor), arguments);

            ExpressionTreeComparer.CheckAreEqualTrees(expected, actualCall);
        }