/// <summary> /// Creates an expression that reads a string from a stream. /// </summary> /// <param name="stream"> /// The lambda parameter that holds the stream. /// </param> /// <returns> /// An expression that returns the string. /// </returns> private static Expression ReadString(ParameterExpression stream) { var bytes = Expression.Parameter(typeof(byte[])); var r = Expression.Condition( ProtocolSerializer.ReadValue(stream, typeof(bool)), Expression.Block( new[] { bytes }, Expression.Assign(bytes, ProtocolSerializer.ReadBytes(stream, ProtocolSerializer.ReadValue(stream, typeof(int)))), Expression.Call( Expression.Constant(Encoding.UTF8), ProtocolSerializer.EncodingGetStringMethod, bytes, Expression.Constant(0), Expression.Property(bytes, nameof(Array.Length)))), Expression.Constant(null, typeof(string))); return(r); }
/// <summary> /// Creates an expression that reads and calls the bit converter. /// </summary> /// <typeparam name="T"> /// The type to convert to. /// </typeparam> /// <param name="bitConverter">The bit converter.</param> /// <param name="stream">The stream.</param> /// <param name="sizeInBytes">The size in bytes.</param> /// <returns> /// The expression. /// </returns> private static Expression ReadAndCallBitConverter <T>(Func <byte[], int, T> bitConverter, ParameterExpression stream, int sizeInBytes) { return(Expression.Call(bitConverter.GetMethodInfo(), ProtocolSerializer.ReadBytes(stream, Expression.Constant(sizeInBytes)), Expression.Constant(0))); }