protected override ExpressionConstruct EmitForEachLoop( ExpressionTreeContext context, CollectionTraits collectionTraits, ExpressionConstruct collection, Func <ExpressionConstruct, ExpressionConstruct> loopBodyEmitter ) { var enumerator = Expression.Variable(collectionTraits.GetEnumeratorMethod.ReturnType, "enumerator"); var current = Expression.Variable(collectionTraits.ElementType, "current"); var moveNextMethod = Metadata._IEnumerator.FindEnumeratorMoveNextMethod(enumerator.Type); var currentProperty = Metadata._IEnumerator.FindEnumeratorCurrentProperty(enumerator.Type, collectionTraits); var endForEach = Expression.Label("END_FOREACH"); return (Expression.Block( new[] { enumerator, current }, Expression.Assign(enumerator, Expression.Call(collection, collectionTraits.GetEnumeratorMethod)), Expression.Loop( Expression.IfThenElse( Expression.Call(enumerator, moveNextMethod), Expression.Block( Expression.Assign(current, Expression.Property(enumerator, currentProperty)), loopBodyEmitter(current) ), Expression.Break(endForEach) ), endForEach ) )); }
protected override ExpressionConstruct EmitEnumFromUnderlyingCastExpression( ExpressionTreeContext context, Type enumType, ExpressionConstruct underlyingValue) { return(Expression.Convert(underlyingValue, enumType)); }
protected override ExpressionConstruct EmitEnumToUnderlyingCastExpression( ExpressionTreeContext context, Type underlyingType, ExpressionConstruct enumValue) { // ExpressionTree cannot handle enum to underlying type conversion... return(Expression.Convert(enumValue, underlyingType)); }
protected override ExpressionConstruct EmitInvokeMethodExpression( ExpressionTreeContext context, ExpressionConstruct instance, MethodInfo method, IEnumerable <ExpressionConstruct> arguments ) { return (instance == null ? Expression.Call(method, arguments.Select(c => c.Expression)) : Expression.Call(instance, method, arguments.Select(c => c.Expression))); }
protected override ExpressionConstruct EmitStringSwitchStatement( ExpressionTreeContext context, ExpressionConstruct target, IDictionary <string, ExpressionConstruct> cases ) { return (Expression.Switch( typeof(void), target, null, Metadata._String.op_Equality, cases.Select(kv => Expression.SwitchCase(kv.Value, Expression.Constant(kv.Key))).ToArray() )); }
protected override ExpressionConstruct EmitAndConditionalExpression( ExpressionTreeContext context, IList <ExpressionConstruct> conditionExpressions, ExpressionConstruct thenExpression, ExpressionConstruct elseExpression ) { return (Expression.IfThenElse( conditionExpressions.Aggregate((l, r) => Expression.AndAlso(l, r)), thenExpression, elseExpression )); }
protected override ExpressionConstruct EmitConditionalExpression( ExpressionTreeContext context, ExpressionConstruct conditionExpression, ExpressionConstruct thenExpression, ExpressionConstruct elseExpression ) { #if DEBUG Contract.Assert( elseExpression == null || (thenExpression.ContextType == elseExpression.ContextType), thenExpression.ContextType + " != " + (elseExpression == null ? "(null)" : elseExpression.ContextType.FullName) ); #endif return (elseExpression == null ? Expression.IfThen(conditionExpression, thenExpression) : thenExpression.ContextType == typeof(void) ? Expression.IfThenElse(conditionExpression, thenExpression, elseExpression) : Expression.Condition(conditionExpression, thenExpression, elseExpression)); }
protected override ExpressionConstruct EmitTryFinally( ExpressionTreeContext context, ExpressionConstruct tryStatement, ExpressionConstruct finallyStatement ) { return(Expression.TryFinally(tryStatement, finallyStatement)); }
protected override ExpressionConstruct EmitThrowExpression( ExpressionTreeContext context, Type expressionType, ExpressionConstruct exceptionExpression ) { return(Expression.Throw(exceptionExpression, expressionType)); }
protected override ExpressionConstruct EmitStoreVariableStatement(ExpressionTreeContext context, ExpressionConstruct variable) { // nop return(null); }
protected override ExpressionConstruct EmitStoreVariableStatement( ExpressionTreeContext context, ExpressionConstruct variable, ExpressionConstruct value ) { return(Expression.Assign(variable, value)); }
protected override ExpressionConstruct EmitNotEqualsExpression( ExpressionTreeContext context, ExpressionConstruct left, ExpressionConstruct right ) { return(Expression.NotEqual(left, right)); }
protected override ExpressionConstruct EmitSetArrayElementStatement(ExpressionTreeContext context, ExpressionConstruct array, ExpressionConstruct index, ExpressionConstruct value) { return (Expression.Assign( Expression.ArrayAccess(array, index), value )); }
protected override ExpressionConstruct EmitForLoop(ExpressionTreeContext context, ExpressionConstruct count, Func <ForLoopContext, ExpressionConstruct> loopBodyEmitter) { var counter = Expression.Variable(typeof(int), "i"); var loopContext = new ForLoopContext(counter); var endFor = Expression.Label("END_FOR"); return (Expression.Block( new[] { counter }, Expression.Loop( Expression.IfThenElse( Expression.LessThan(counter, count), Expression.Block( loopBodyEmitter(loopContext), Expression.Assign(counter, Expression.Increment(counter)) ), Expression.Break(endFor) ), endFor ) )); }
protected override void EmitMethodEpilogue(ExpressionTreeContext context, EnumSerializerMethod method, ExpressionConstruct construct) { if (construct == null) { return; } context.SetDelegate(method, EmitMethodEpilogue(context, ExpressionTreeContext.CreateDelegateType <TObject>(method), method, construct)); }
protected override ExpressionConstruct EmitGetPropretyExpression( ExpressionTreeContext context, ExpressionConstruct instance, PropertyInfo property ) { return(Expression.Property(instance, property)); }
protected override ExpressionConstruct EmitUnboxAnyExpression(ExpressionTreeContext context, Type targetType, ExpressionConstruct value) { return(Expression.Convert(value, targetType)); }
protected override ExpressionConstruct EmitInvokeVoidMethod( ExpressionTreeContext context, ExpressionConstruct instance, MethodInfo method, params ExpressionConstruct[] arguments ) { return(this.EmitInvokeMethodExpression(context, instance, method, arguments)); }
protected override ExpressionConstruct EmitCreateNewObjectExpression( ExpressionTreeContext context, ExpressionConstruct variable, ConstructorInfo constructor, params ExpressionConstruct[] arguments ) { return(Expression.New(constructor, arguments.Select(c => c.Expression))); }
protected override ExpressionConstruct EmitIncrement(ExpressionTreeContext context, ExpressionConstruct int32Value) { return(Expression.Assign(int32Value, Expression.Increment(int32Value))); }
protected override ExpressionConstruct EmitLessThanExpression( ExpressionTreeContext context, ExpressionConstruct left, ExpressionConstruct right ) { return(Expression.LessThan(left, right)); }
protected override void EmitMethodEpilogue(ExpressionTreeContext context, CollectionSerializerMethod method, ExpressionConstruct construct) { if (construct == null) { return; } context.SetDelegate(method, this.EmitMethodEpilogue(context, ExpressionTreeContext.CreateDelegateType <TObject>(method, SerializerClass, CollectionTraitsOfThis), method, construct)); }
protected override ExpressionConstruct EmitSetField( ExpressionTreeContext context, ExpressionConstruct instance, FieldInfo field, ExpressionConstruct value ) { return(Expression.Assign(Expression.Field(instance, field), value)); }
protected override ExpressionConstruct EmitBoxExpression(ExpressionTreeContext context, Type valueType, ExpressionConstruct value) { return(Expression.Convert(value, typeof(object))); }
protected override ExpressionConstruct EmitLoadVariableExpression(ExpressionTreeContext context, ExpressionConstruct variable) { // Just use ParameterExpression. #if DEBUG Contract.Assert( (variable.Expression is ParameterExpression) && variable.ContextType != typeof(void), variable.Expression.ToString() ); #endif return(new ExpressionConstruct(variable, true)); }
protected override ExpressionConstruct EmitGetFieldExpression(ExpressionTreeContext context, ExpressionConstruct instance, FieldInfo field) { return(Expression.Field(instance, field)); }
protected override ExpressionConstruct EmitNotExpression(ExpressionTreeContext context, ExpressionConstruct booleanExpression) { return(Expression.Not(booleanExpression)); }
private Delegate EmitMethodEpilogue <T>(ExpressionTreeContext context, Type delegateType, T method, ExpressionConstruct construct) { if (SerializerDebugging.TraceEnabled) { SerializerDebugging.TraceEvent("----{0}----", method); construct.ToString(SerializerDebugging.ILTraceWriter); SerializerDebugging.FlushTraceData(); } var lambda = Expression.Lambda( delegateType, construct.Expression, method.ToString(), false, context.GetCurrentParameters() ); #if !NETFX_CORE && !SILVERLIGHT if (SerializerDebugging.DumpEnabled) { var mb = this._typeBuilder.DefineMethod( method.ToString(), MethodAttributes.Public | MethodAttributes.Static, lambda.Type, lambda.Parameters.Select(e => e.Type).ToArray() ); lambda.CompileToMethod(mb); } #endif return(lambda.Compile()); }
protected override ExpressionConstruct EmitSetProprety( ExpressionTreeContext context, ExpressionConstruct instance, PropertyInfo property, ExpressionConstruct value ) { return(Expression.Assign(Expression.Property(instance, property), value)); }