示例#1
0
        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
                         )
                     ));
        }
示例#2
0
 protected override ExpressionConstruct EmitEnumFromUnderlyingCastExpression(
     ExpressionTreeContext context,
     Type enumType,
     ExpressionConstruct underlyingValue)
 {
     return(Expression.Convert(underlyingValue, enumType));
 }
示例#3
0
 protected override ExpressionConstruct EmitEnumToUnderlyingCastExpression(
     ExpressionTreeContext context,
     Type underlyingType,
     ExpressionConstruct enumValue)
 {
     // ExpressionTree cannot handle enum to underlying type conversion...
     return(Expression.Convert(enumValue, underlyingType));
 }
示例#4
0
 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()
              ));
 }
示例#6
0
 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
              ));
 }
示例#7
0
        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));
        }
示例#8
0
 protected override ExpressionConstruct EmitTryFinally(
     ExpressionTreeContext context, ExpressionConstruct tryStatement, ExpressionConstruct finallyStatement
     )
 {
     return(Expression.TryFinally(tryStatement, finallyStatement));
 }
示例#9
0
 protected override ExpressionConstruct EmitThrowExpression(
     ExpressionTreeContext context, Type expressionType, ExpressionConstruct exceptionExpression
     )
 {
     return(Expression.Throw(exceptionExpression, expressionType));
 }
示例#10
0
 protected override ExpressionConstruct EmitStoreVariableStatement(ExpressionTreeContext context, ExpressionConstruct variable)
 {
     // nop
     return(null);
 }
示例#11
0
 protected override ExpressionConstruct EmitStoreVariableStatement(
     ExpressionTreeContext context, ExpressionConstruct variable, ExpressionConstruct value
     )
 {
     return(Expression.Assign(variable, value));
 }
示例#12
0
 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
              ));
 }
示例#14
0
        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
                         )
                     ));
        }
示例#15
0
        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));
        }
示例#16
0
 protected override ExpressionConstruct EmitGetPropretyExpression(
     ExpressionTreeContext context, ExpressionConstruct instance, PropertyInfo property
     )
 {
     return(Expression.Property(instance, property));
 }
示例#17
0
 protected override ExpressionConstruct EmitUnboxAnyExpression(ExpressionTreeContext context, Type targetType, ExpressionConstruct value)
 {
     return(Expression.Convert(value, targetType));
 }
示例#18
0
 protected override ExpressionConstruct EmitInvokeVoidMethod(
     ExpressionTreeContext context, ExpressionConstruct instance, MethodInfo method, params ExpressionConstruct[] arguments
     )
 {
     return(this.EmitInvokeMethodExpression(context, instance, method, arguments));
 }
示例#19
0
 protected override ExpressionConstruct EmitCreateNewObjectExpression(
     ExpressionTreeContext context, ExpressionConstruct variable, ConstructorInfo constructor, params ExpressionConstruct[] arguments
     )
 {
     return(Expression.New(constructor, arguments.Select(c => c.Expression)));
 }
示例#20
0
 protected override ExpressionConstruct EmitIncrement(ExpressionTreeContext context, ExpressionConstruct int32Value)
 {
     return(Expression.Assign(int32Value, Expression.Increment(int32Value)));
 }
示例#21
0
 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));
        }
示例#23
0
 protected override ExpressionConstruct EmitSetField(
     ExpressionTreeContext context, ExpressionConstruct instance, FieldInfo field, ExpressionConstruct value
     )
 {
     return(Expression.Assign(Expression.Field(instance, field), value));
 }
示例#24
0
 protected override ExpressionConstruct EmitBoxExpression(ExpressionTreeContext context, Type valueType, ExpressionConstruct value)
 {
     return(Expression.Convert(value, typeof(object)));
 }
示例#25
0
        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));
        }
示例#26
0
 protected override ExpressionConstruct EmitGetFieldExpression(ExpressionTreeContext context, ExpressionConstruct instance, FieldInfo field)
 {
     return(Expression.Field(instance, field));
 }
示例#27
0
 protected override ExpressionConstruct EmitNotExpression(ExpressionTreeContext context, ExpressionConstruct booleanExpression)
 {
     return(Expression.Not(booleanExpression));
 }
示例#28
0
        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());
        }
示例#29
0
 protected override ExpressionConstruct EmitSetProprety(
     ExpressionTreeContext context, ExpressionConstruct instance, PropertyInfo property, ExpressionConstruct value
     )
 {
     return(Expression.Assign(Expression.Property(instance, property), value));
 }