protected override Expression BuildAfterExpression(ExpressionBuilderArgs args, ParameterExpression innerResultVar) { var expressions = new List <Expression>(); expressions.Add(base.BuildAfterExpression(args, innerResultVar)); if (_convertValue != null) { expressions.Add(Expression.Assign(innerResultVar, Invoke(_convertValue, args, innerResultVar))); } if (_assertValue != null) { expressions.Add(Expression.IfThen( Expression.Not(Invoke(_assertValue, args, innerResultVar)), Expression.Throw(Expression.New( typeof(BinaryReadingAssertException).GetConstructor(new[] { typeof(Object), typeof(Object), typeof(String) }), Expression.TypeAs(args.InstanceVar, typeof(Object)), Expression.TypeAs(innerResultVar, typeof(Object)), Expression.Constant(MemberName))))); } if (_positionAfter != null) { expressions.Add(Expression.Assign(AdvancedExpression.Position(args.BrParameter), Invoke(_positionAfter, args, innerResultVar))); } return(Expression.Block(expressions)); }
private Expression <ReadDelegate <T> > GetLambdaExpression <T>() { var brParameter = Expression.Parameter(typeof(BitsReader), "br"); Dictionary <Type, Expression <ReadFunc> > typeReaders = Types.ToDictionary(pair => pair.Key, pair => { var args = new ExpressionBuilderArgs(); args.BrParameter = brParameter; args.InstanceVar = Expression.Parameter(typeof(Object)); args.TypeReaders = Expression.Parameter(typeof(Dictionary <Type, Expression <ReadFunc> >)); args.Endianness = Expression.Constant(_endianness); var expression = pair.Value.BuildExpression(args); return(Expression.Lambda <ReadFunc>(expression, string.Format("Read_{0}", pair.Key.Name), new[] { args.BrParameter, args.InstanceVar, args.TypeReaders })); }); var resultVar = Expression.Variable(typeof(T), "root"); var brCheckExpression = Expression.IfThen(Expression.Equal(brParameter, Expression.Constant(null)), Expression.Throw(Expression.New(typeof(ArgumentNullException).GetConstructor(new[] { typeof(String) }), Expression.Constant(brParameter.Name, typeof(String))))); var ctorExpression = Expression.Assign(resultVar, Expression.New(typeof(T))); var typeReadersVar = Expression.Variable(typeof(Dictionary <Type, Expression <ReadFunc> >)); var typeReadExpression = Expression.Block(new[] { resultVar, typeReadersVar }, brCheckExpression, CreateTypeReaders(typeReadersVar, typeReaders), ctorExpression, Expression.Invoke(AdvancedExpression.GetTypeBuilder(typeReadersVar, Expression.Constant(typeof(T))), brParameter, resultVar, typeReadersVar), resultVar); return(Expression.Lambda <ReadDelegate <T> >(typeReadExpression, string.Format("Read_Root", typeof(T).Name), new[] { brParameter })); }
private Expression BuildStructExpression(ExpressionBuilderArgs args) { Expression readExpression; if (Size != null) { readExpression = Expression.Call( typeof(BitsReaderExtensions) .GetMethod("ReadStruct", new[] { typeof(BitsReader), typeof(BinarySize), typeof(Endianness) }) .MakeGenericMethod(MemberType), args.BrParameter, Invoke(Size, args), args.Endianness); } else { readExpression = Expression.Call( typeof(BitsReaderExtensions) .GetMethod("ReadStruct", new[] { typeof(BitsReader), typeof(Endianness) }) .MakeGenericMethod(MemberType), args.BrParameter, args.Endianness); } return(readExpression); }
public Expression BuildExpression(ExpressionBuilderArgs args) { var members = GetMembers(); var resultObjectVar = args.InstanceVar; var innerArgs = args.Clone(); if (_endianness.HasValue) { innerArgs.Endianness = Expression.Constant(_endianness.Value, typeof(Endianness)); } var instanceVar = Expression.Parameter(typeof(T), "result"); var expressions = new List <Expression>(); expressions.Add(Expression.Call(typeof(Debug), "Indent", null)); expressions.Add(Expression.Assign(instanceVar, Expression.Convert(resultObjectVar, typeof(T)))); if (typeof(T).IsClass()) { expressions.Add(Expression.IfThen(Expression.Equal(instanceVar, Expression.Constant(null)), Expression.Throw(Expression.New(typeof(ArgumentNullException).GetConstructor(new[] { typeof(String) }), Expression.Constant(instanceVar.Name, typeof(String)))))); } innerArgs.InstanceVar = instanceVar; expressions.AddRange(members // filter ignored members .Where(pair => pair.Value != null) .Select(pair => { var expression = pair.Value.BuildExpression(innerArgs); return((Expression)Expression.Assign( Expression.PropertyOrField(instanceVar, pair.Value.MemberName), expression)); })); expressions.Add(Expression.Call(typeof(Debug), "Unindent", null)); return(Expression.Block(new[] { instanceVar }, expressions)); }
protected override Expression BuildBodyExpression(ExpressionBuilderArgs args, ParameterExpression innerResultVar, ParameterExpression typeVar) { if (_size == null) { throw new InvalidMappingException("Size of skipped member must be set."); } return(Expression.AddAssign(AdvancedExpression.Position(args.BrParameter), Expression.Convert(Invoke(_size, args), typeof(BinaryOffset)))); }
protected override Expression BuildBeforeExpression(ExpressionBuilderArgs args) { var expressions = new List <Expression>(); if (_position != null) { expressions.Add(Expression.Assign(AdvancedExpression.Position(args.BrParameter), Invoke(_position, args))); } expressions.Add(base.BuildBeforeExpression(args)); return(Expression.Block(expressions)); }
protected override Expression BuildCtorExpression(ExpressionBuilderArgs args) { if (_factory != null) { return(Expression.Convert(Expression.Invoke(_factory, AdvancedExpression.Context <T>(args.BrParameter, args.InstanceVar)), MemberType)); } else { return(Expression.New(MemberType)); } }
protected override Expression BuildCtorExpression(ExpressionBuilderArgs args) { var elementType = MemberType.GetElementType(); var lengthVar = Expression.Variable(typeof(UInt64), "length"); var ctorExp = Expression.NewArrayBounds( elementType, lengthVar); return(Expression.Block(new [] { lengthVar }, Expression.Assign(lengthVar, Invoke(_length, args)), AdvancedExpression.Debug(string.Format("Array {0} initialized. ({{0}} elements).", MemberName), lengthVar), ctorExp)); }
protected override Expression BuildBodyExpression(ExpressionBuilderArgs args, ParameterExpression innerResultVar, ParameterExpression typeVar) { if (_encoding == null) { throw new InvalidMappingException(string.Format("No encoding specified for {0}", innerResultVar.Name)); } return(Expression.Assign(innerResultVar, Expression.Call( typeof(BitsReaderExtensions) .GetMethod("ReadString", new[] { typeof(BitsReader), typeof(BinarySize), typeof(Encoding) }), args.BrParameter, Invoke(Size, args), Invoke(_encoding, args)))); }
protected override Expression BuildBodyExpression(ExpressionBuilderArgs args, ParameterExpression innerResultVar, ParameterExpression typeVar) { var elementType = MemberType.GetElementType(); if (elementType == typeof(Byte)) { return(Expression.Call( args.BrParameter, "ReadBits", null, Expression.New( typeof(BinarySize).GetConstructor(new[] { typeof(UInt64), typeof(Byte) }), Invoke(_length, args), Expression.Constant((Byte)0, typeof(Byte))), Expression.Constant(null, typeof(Endianness?)))); } else { return(base.BuildBodyExpression(args, innerResultVar, typeVar)); } }
protected override Expression BuildBodyExpression(ExpressionBuilderArgs args, ParameterExpression innerResultVar, ParameterExpression typeVar) { var elementType = typeof(TElement); //IExpressionBuilder typeBuilder = FileFormatBuilder.Types[elementType]; var iVar = Expression.Variable(typeof(int), "i"); var elementVar = Expression.Variable(elementType, "iterator"); var loopEndLabel = Expression.Label(); var elementBuilder = MemberBuilder <TMember, TElement> .Create("[i]"); if (_elementBuilderConfiguration != null) { _elementBuilderConfiguration(elementBuilder); } var innerArgs = args.Clone(); innerArgs.InstanceVar = innerResultVar; return(Expression.Block( new[] { iVar, elementVar }, Expression.Assign(iVar, Expression.Constant(0)), AdvancedExpression.Debug("Reading collection {0}...", Expression.Constant(MemberName)), Expression.Loop( Expression.IfThenElse( GetLoopCondition(args, innerResultVar, iVar, elementVar), Expression.Block( AdvancedExpression.Debug("Reading {0}[{1}]...", Expression.Constant(MemberName), iVar), Expression.Assign(elementVar, elementBuilder.BuildExpression(innerArgs)), /* * Expression.Assign(elementVar, elementCtor), * typeBuilder.BuildExpression(brParameter, elementVar, typeReaders), */ InsertElement(args, innerResultVar, iVar, elementVar), Expression.PostIncrementAssign(iVar)), Expression.Break(loopEndLabel)), loopEndLabel))); }
protected override Expression GetLoopCondition(ExpressionBuilderArgs args, ParameterExpression innerResultVar, ParameterExpression iVar, ParameterExpression iteratorVar) { return(Expression.Not(Invoke(_lastElementWhen, args, innerResultVar, iVar))); }
protected override Expression BuildCtorExpression(ExpressionBuilderArgs args) { return(Expression.Constant(String.Empty)); }
protected Expression Invoke <TResult>(Expression <Func <IMemberContext <T, TMember>, TResult> > expression, ExpressionBuilderArgs args, ParameterExpression innerResultVar) { return(Expression.Invoke(expression, AdvancedExpression.MemberContext <T, TMember>(args.BrParameter, args.InstanceVar, innerResultVar))); }
protected override Expression BuildBodyExpression(ExpressionBuilderArgs args, ParameterExpression innerResultVar, ParameterExpression typeVar) { return(Expression.Assign(innerResultVar, BuildStructExpression(args))); }
protected override Expression InsertElement(ExpressionBuilderArgs args, ParameterExpression innerResultVar, ParameterExpression iVar, ParameterExpression iteratorVar) { return(Expression.Call(innerResultVar, "Add", null, iteratorVar)); }
protected override Expression GetLoopCondition(ExpressionBuilderArgs args, ParameterExpression innerResultVar, ParameterExpression iVar, ParameterExpression iteratorVar) { return(Expression.LessThan(iVar, Expression.ArrayLength(innerResultVar))); }
protected override Expression InsertElement(ExpressionBuilderArgs args, ParameterExpression innerResultVar, ParameterExpression iVar, ParameterExpression iteratorVar) { return(Expression.Assign(Expression.ArrayAccess(innerResultVar, iVar), iteratorVar)); }
protected abstract Expression InsertElement(ExpressionBuilderArgs args, ParameterExpression innerResultVar, ParameterExpression iVar, ParameterExpression iteratorVar);
protected override Expression BuildBodyExpression(ExpressionBuilderArgs args, ParameterExpression innerResultVar, ParameterExpression typeVar) { return(Expression.Block(AdvancedExpression.Debug("Type: {0}", typeVar), Expression.Invoke(AdvancedExpression.GetTypeBuilder(args.TypeReaders, typeVar), args.BrParameter, innerResultVar, args.TypeReaders))); }
protected abstract Expression GetLoopCondition(ExpressionBuilderArgs args, ParameterExpression innerResultVar, ParameterExpression iVar, ParameterExpression iteratorVar);
protected override Expression BuildCtorExpression(ExpressionBuilderArgs args) { return(Expression.Default(typeof(TMember))); }