public Expression Deserialize(FormatContext context) { var genericArgs = context.Type.GetGenericArguments(); //new KeyValuePair<TKey, TValue>(Read(TKey), Read(TValue)) return(New(context.Type.GetConstructor(genericArgs), context.Read(genericArgs[0]), context.Read(genericArgs[1]))); }
public Expression Deserialize(FormatContext context) { var collection = context.Type.GetInterface("ICollection`1") ?? context.Type.GetInterface("IReadOnlyCollection`1") ?? context.Type; var itemType = collection.GetGenericArguments()[0]; var countVar = Variable(typeof(int), "_count"); var resultVar = Variable(context.Type, "_ret"); var indexVar = Variable(typeof(int), "_idx"); var breakLabel = Label("_break"); return(Block(new[] { countVar, resultVar, indexVar }, Assign(indexVar, Constant(0)), Assign(countVar, context.Read <int>()), Assign(resultVar, New(context.DeserializableType ?? context.Type)), Loop(IfThenElse( LessThan(indexVar, countVar), Block( Call(Convert(resultVar, context.DeserializableType ?? collection), "Add", null, context.Read(itemType)), PostIncrementAssign(indexVar)), Break(breakLabel)), breakLabel), resultVar )); }
public Expression Deserialize(FormatContext context) { var t = context.Type; var arrType = t.IsArray ? t.GetElementType() : t.GetGenericArguments()[0]; var indexVar = Variable(typeof(int), "_idx"); var arrLengthVar = Variable(typeof(int), "_len"); var resultVar = Variable(t, "_ret"); //t is an array type var breakLabel = Label("_break"); //i = 0; //length = msg.ReadInt32(); //result = new T[length]; // //while (true) //{ // if (index < length) // { // result[i] = Read(T); // i++; // } // else // { // break; // } //} return(Block(new[] { indexVar, arrLengthVar, resultVar }, Assign(indexVar, Constant(0)), Assign(arrLengthVar, context.Read <int>()), Assign(resultVar, t.IsArray ? (Expression)NewArrayBounds(arrType, arrLengthVar) : New(t)), Loop(IfThenElse( LessThan(indexVar, arrLengthVar), Block( t.IsArray ? (Expression)Assign(ArrayAccess(resultVar, indexVar), context.Read(arrType)) : Call(resultVar, "Add", null, context.Read(arrType)), PostIncrementAssign(indexVar)), Break(breakLabel)), breakLabel), resultVar)); }
public Expression Deserialize(FormatContext context) { return(Expression.New( typeof(Guid).GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, new[] { typeof(byte[]) }, null), context.Read <byte[]>())); }
public Expression Deserialize(FormatContext context) { var enumType = context.Type.GetEnumUnderlyingType(); return(Convert(context.Read(enumType), context.Type)); }
public Expression Deserialize(FormatContext context) { var genericArgs = context.Type.GetGenericArguments(); //new ValueTuple<T1, T2, ..., T3>(Read(T1), Read(T2), ..., Read(TN)) return(New(context.Type.GetConstructor(genericArgs), genericArgs.Select(o => context.Read(o)))); }