public void WireConstructorWithInitialization( MutableConstructorInfo constructor, Tuple <FieldInfo, MethodInfo> initializationMembers, IProxySerializationEnabler proxySerializationEnabler) { ArgumentUtility.CheckNotNull("constructor", constructor); ArgumentUtility.CheckNotNull("proxySerializationEnabler", proxySerializationEnabler); if (initializationMembers == null || proxySerializationEnabler.IsDeserializationConstructor(constructor)) { return; } // We cannot protect the decrement with try-finally because the this pointer must be initialized before entering a try block. // Using *IncrementAssign and *DecrementAssign results in un-verifiable code (emits stloc.0). constructor.SetBody( ctx => { var initilizationMethod = initializationMembers.Item2; var counter = Expression.Field(ctx.This, initializationMembers.Item1); var one = Expression.Constant(1); var zero = Expression.Constant(0); return(Expression.Block( Expression.Assign(counter, Expression.Add(counter, one)), constructor.Body, Expression.Assign(counter, Expression.Subtract(counter, one)), Expression.IfThen( Expression.Equal(counter, zero), Expression.Call(ctx.This, initilizationMethod, Expression.Constant(InitializationSemantics.Construction))))); }); }
private void AdaptDeserializationConstructor(MutableConstructorInfo constructor, Tuple <string, FieldInfo>[] serializedFieldMapping) { constructor .SetBody( ctx => Expression.Block( typeof(void), new[] { ctx.PreviousBody }.Concat(BuildFieldDeserializationExpressions(ctx.This, ctx.Parameters[0], serializedFieldMapping)))); }
public void SetBody() { var fakeBody = ExpressionTreeObjectMother.GetSomeExpression(typeof(object)); Func <ConstructorBodyModificationContext, Expression> bodyProvider = context => { Assert.That(_constructor.ParameterExpressions, Is.Not.Empty); Assert.That(context.Parameters, Is.EqualTo(_constructor.ParameterExpressions)); Assert.That(context.DeclaringType, Is.SameAs(_constructor.DeclaringType)); Assert.That(context.IsStatic, Is.False); Assert.That(context.PreviousBody, Is.SameAs(_constructor.Body)); return(fakeBody); }; _constructor.SetBody(bodyProvider); var expectedBody = Expression.Block(typeof(void), fakeBody); ExpressionTreeComparer.CheckAreEqualTrees(expectedBody, _constructor.Body); }
public void IsModified_Construcotr_ChangedBody() { _addedCtor.SetBody(ctx => Expression.Empty()); Assert.That(_tracker.IsModified(), Is.True); }
public static void ModifyConstructor(MutableConstructorInfo mutableConstructor) { mutableConstructor.SetBody(ctx => ExpressionTreeObjectMother.GetSomeExpression(typeof(void))); }