/// <summary> /// Applies a protocol-buffer stream to an existing instance, using length-prefixed /// data - useful with network IO. /// </summary> /// <typeparam name="T">The type being merged.</typeparam> /// <param name="instance">The existing instance to be modified (can be null).</param> /// <param name="source">The binary stream to apply to the instance (cannot be null).</param> /// <param name="style">How to encode the length prefix.</param> /// <returns>The updated instance; this may be different to the instance argument if /// either the original instance was null, or the stream defines a known sub-type of the /// original instance.</returns> public static T MergeWithLengthPrefix <T>(Stream source, T instance, PrefixStyle style) { RuntimeTypeModel model = RuntimeTypeModel.Default; return((T)model.DeserializeWithLengthPrefix(source, instance, model.MapType(typeof(T)), style, 0)); }
/// <summary> /// Creates a new instance from a protocol-buffer stream that has a length-prefix /// on data (to assist with network IO). /// </summary> /// <typeparam name="T">The type to be created.</typeparam> /// <param name="source">The binary stream to apply to the new instance (cannot be null).</param> /// <param name="style">How to encode the length prefix.</param> /// <param name="fieldNumber">The expected tag of the item (only used with base-128 prefix style).</param> /// <returns>A new, initialized instance.</returns> public static T DeserializeWithLengthPrefix <T>(Stream source, PrefixStyle style, int fieldNumber) { RuntimeTypeModel model = RuntimeTypeModel.Default; return((T)model.DeserializeWithLengthPrefix(source, null, model.MapType(typeof(T)), style, fieldNumber)); }
public static void PrepareSerializer <T>() { RuntimeTypeModel @default = RuntimeTypeModel.Default; @default[@default.MapType(typeof(T))].CompileInPlace(); }
public static void SerializeWithLengthPrefix <T>(Stream destination, T instance, PrefixStyle style, int fieldNumber) { RuntimeTypeModel @default = RuntimeTypeModel.Default; @default.SerializeWithLengthPrefix(destination, instance, @default.MapType(typeof(T)), style, fieldNumber); }
/// <summary> /// Writes a protocol-buffer representation of the given instance to the supplied stream, /// with a length-prefix. This is useful for socket programming, /// as DeserializeWithLengthPrefix/MergeWithLengthPrefix can be used to read the single object back /// from an ongoing stream. /// </summary> /// <param name="instance">The existing instance to be serialized (cannot be null).</param> /// <param name="style">How to encode the length prefix.</param> /// <param name="destination">The destination stream to write to.</param> /// <param name="fieldNumber">The tag used as a prefix to each record (only used with base-128 style prefixes).</param> public static void SerializeWithLengthPrefix(Stream destination, object instance, PrefixStyle style, int fieldNumber) { RuntimeTypeModel model = RuntimeTypeModel.Default; model.SerializeWithLengthPrefix(destination, instance, model.MapType(instance.GetType()), style, fieldNumber); }