private static EditableExpression CheckSerializationInternal(EditableExpression mutableLambda) { DataContractSerializer dcs = new DataContractSerializer(mutableLambda.GetType()); MemoryStream ms = new MemoryStream(); XmlDictionaryWriter xdw = XmlDictionaryWriter.CreateTextWriter(ms, Encoding.UTF8, true); //dcs.WriteObject(xdw, mutableLambda); XmlSerializer xs = new XmlSerializer(mutableLambda.GetType()); xs.Serialize(ms, mutableLambda); xdw.Flush(); ms.Flush(); string str = Encoding.UTF8.GetString(ms.ToArray()); MemoryStream ms2 = new MemoryStream(Encoding.UTF8.GetBytes(str)); //Object o = dcs.ReadObject(ms2); object o = xs.Deserialize(ms2); if (o is EditableExpression) { return(o as EditableExpression); } else { return(null); } }
public void ExpressionSearchIsTransmittedCorrectly() { //send the expression, then test it against the one received to make sure they're identical Expression <Func <Instrument, bool> > exp = x => x.Symbol == "SPY" && x.Type == InstrumentType.CFD && !x.IsContinuousFuture; var ms = new MemoryStream(); EditableExpression editableExpr = EditableExpression.CreateEditableExpression(exp); XmlSerializer xs = new XmlSerializer(editableExpr.GetType()); xs.Serialize(ms, editableExpr); Expression <Func <Instrument, bool> > receivedExpr = null; _instrumentSourceMock.Setup(x => x.FindInstruments(It.IsAny <Expression <Func <Instrument, bool> > >(), It.IsAny <MyDBContext>())) .Callback <Expression <Func <Instrument, bool> >, MyDBContext>((x, y) => receivedExpr = x); _client.FindInstruments(exp); Thread.Sleep(100); Assert.IsNotNull(receivedExpr); var ms2 = new MemoryStream(); EditableExpression receivedEditableExpr = EditableExpression.CreateEditableExpression(exp); xs.Serialize(ms2, receivedEditableExpr); Assert.AreEqual(Encoding.UTF8.GetString(ms.ToArray()), Encoding.UTF8.GetString(ms2.ToArray())); }