public void CreateBody_BuildsCorrectBody_UsingAbstractTemplateMembers() { var accessorImplementationMethod = NormalizingMemberInfoFromExpressionUtility.GetGenericMethodDefinition((PropertyAccessor a) => a.SetValue <object> (null)); var arguments = new Expression[] { Expression.Parameter(typeof(int), "param") }; var ctx = new MethodBodyModificationContext(_proxyType, false, new ParameterExpression[0], Type.EmptyTypes, typeof(int), null, null); _interceptorPartialMock .Stub(stub => PrivateInvoke.GetNonPublicProperty(stub, "AccessorImplementationMethod")) .Return(accessorImplementationMethod); _interceptorPartialMock .Stub(stub => PrivateInvoke.InvokeNonPublicMethod(stub, "GetArguments", Arg <MethodBaseBodyContextBase> .Is.Anything)) .WhenCalled(mi => Assert.That(mi.Arguments[0], Is.SameAs(ctx))) .Return(arguments); var result = (Expression)PrivateInvoke.InvokeNonPublicMethod(_interceptorPartialMock, "CreateBody", ctx); var expectedbody = Expression.Call( Expression.Call( Expression.Property( new ThisExpression(_proxyType), typeof(DomainObject).GetProperty("Properties", BindingFlags.Instance | BindingFlags.NonPublic)), "get_Item", null, Expression.Constant("abc")), "SetValue", new[] { typeof(int) }, arguments); ExpressionTreeComparer.CheckAreEqualTrees(expectedbody, result); }
protected override Expression CreateBody(MethodBodyModificationContext ctx) { var propertyIndexer = Expression.Property(ctx.This, s_properties); var propertyAccessor = Expression.Call(propertyIndexer, s_getPropertyAccessor, Expression.Constant(_propertyName)); var body = Expression.Call(propertyAccessor, AccessorImplementationMethod.MakeGenericMethod(_propertyType), GetArguments(ctx)); return(body); }
public void SetUp() { _declaringType = MutableTypeObjectMother.Create(); _isStatic = BooleanObjectMother.GetRandomBoolean(); _parameters = new[] { Expression.Parameter(typeof(int)), Expression.Parameter(typeof(object)) }; _baseMethod = ReflectionObjectMother.GetSomeMethod(); _genericParameters = new[] { ReflectionObjectMother.GetSomeGenericParameter() }; _returnType = ReflectionObjectMother.GetSomeType(); _previousBody = Expression.Block(_parameters[0], _parameters[1]); _context = new MethodBodyModificationContext( _declaringType, _isStatic, _parameters.AsOneTime(), _genericParameters.AsOneTime(), _returnType, _baseMethod, _previousBody); _contextWithoutPreviousBody = new MethodBodyModificationContext( _declaringType, _isStatic, _parameters, _genericParameters, _returnType, _baseMethod, null); }
public void SetBody(Func <MethodBodyModificationContext, Expression> bodyProvider) { ArgumentUtility.CheckNotNull("bodyProvider", bodyProvider); var context = new MethodBodyModificationContext( MutableDeclaringType, IsStatic, _parameterExpressions, _genericParameters.Cast <Type>(), ReturnType, _baseMethod, _body); var newBody = BodyProviderUtility.GetTypedBody(ReturnType, bodyProvider, context); var oldBody = _body; _body = newBody; if (BodyChanged != null) { BodyChanged(this, new BodyChangedEventArgs(oldBody, _body)); } }
protected abstract IEnumerable <Expression> GetArguments(MethodBodyModificationContext ctx);
protected virtual Expression CreateBody(MethodBodyModificationContext ctx) { return(ctx.PreviousBody); }
protected override IEnumerable <Expression> GetArguments(MethodBodyModificationContext ctx) { // PropertyAccessor.GetValue<T> does not take any arguments. return(new Expression[0]); }
protected override IEnumerable <Expression> GetArguments(MethodBodyModificationContext ctx) { // PropertyAccessor.SetValue<T> takes one argument. Provide the last (which is the 'value' parameter of a property or indexer). return(new Expression[] { ctx.Parameters.Last() }); }