public Expression Serialize(FormatContextWithValue context) { var genericArgs = context.Type.GetGenericArguments(); //Write(value.Key); //Write(value.Value); return(Block( context.Write(genericArgs[0], Property(context.Value, "Key")), context.Write(genericArgs[1], Property(context.Value, "Value")))); }
public Expression Serialize(FormatContextWithValue context) { var collection = context.Type.GetInterface("ICollection`1") ?? context.Type.GetInterface("IReadOnlyCollection`1") ?? context.Type; var itemType = collection.GetGenericArguments()[0]; var enumerator = Variable(typeof(IEnumerator)); var breakLabel = Label("_break"); return(Block(new[] { enumerator }, Assign(enumerator, Call(Convert(context.Value, typeof(IEnumerable)), "GetEnumerator", null)), context.Write <int>(Property(context.Value, "Count")), Loop(IfThenElse( IsTrue(Call(enumerator, "MoveNext", null)), context.Write(itemType, Convert(Property(enumerator, "Current"), itemType)), Break(breakLabel) ), breakLabel))); }
public Expression Serialize(FormatContextWithValue context) { var block = new List <Expression>(); int i = 1; //Write(value.Item1); //Write(value.Item2); //... //Write(value.ItemN); foreach (var item in context.Type.GetGenericArguments()) { block.Add(context.Write(item, Field(context.Value, "Item" + i++))); } return(Block(block)); }
public Expression Serialize(FormatContextWithValue context) { var t = context.Type; var itemType = t.IsArray ? t.GetElementType() : t.GetGenericArguments()[0]; var indexVar = Variable(typeof(int), "_idx"); var arrLength = Property(context.Value, t.IsArray ? "Length" : "Count"); var breakLabel = Label("_break"); var block = new List <Expression> { //msg.Write(length); context.Formats.Get(typeof(int)).Serialize(context.WithType(typeof(int)).WithValue(arrLength)), //i = 0; Assign(indexVar, Constant(0)), //while (true) //{ // if (index < length) // { // Write(value[i]); // i++; // } // else // { // break; // } //} Loop(IfThenElse( LessThan(indexVar, arrLength), Block( context.Write(itemType, t.IsArray ? (Expression)ArrayAccess(context.Value, indexVar) : Call(context.Value, "get_Item", null, indexVar)), PostIncrementAssign(indexVar)), Break(breakLabel)), breakLabel) }; return(Block(new[] { indexVar }, block)); }
public Expression Serialize(FormatContextWithValue context) { return(context.Write <byte[]>(Expression.Call(context.Value, nameof(Guid.ToByteArray), null))); }
public Expression Serialize(FormatContextWithValue context) { var enumType = context.Type.GetEnumUnderlyingType(); return(context.Write(enumType, Convert(context.Value, enumType))); }