| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- using System;
- using System.ComponentModel;
- using System.IO;
- using ProtoBuf;
- namespace Base
- {
- public static class ProtobufHelper
- {
- public static byte[] ToBytes(object message)
- {
- using (MemoryStream ms = new MemoryStream())
- {
- Serializer.Serialize(ms, message);
- return ms.ToArray();
- }
- }
- public static T FromBytes<T>(byte[] bytes)
- {
- T t;
- using (MemoryStream ms = new MemoryStream(bytes, 0, bytes.Length))
- {
- t = Serializer.Deserialize<T>(ms);
- }
- ISupportInitialize iSupportInitialize = t as ISupportInitialize;
- if (iSupportInitialize == null)
- {
- return t;
- }
- iSupportInitialize.EndInit();
- return t;
- }
- public static T FromBytes<T>(byte[] bytes, int index, int length)
- {
- T t;
- using (MemoryStream ms = new MemoryStream(bytes, index, length))
- {
- t = Serializer.Deserialize<T>(ms);
- }
- ISupportInitialize iSupportInitialize = t as ISupportInitialize;
- if (iSupportInitialize == null)
- {
- return t;
- }
- iSupportInitialize.EndInit();
- return t;
- }
- public static object FromBytes(Type type, byte[] bytes)
- {
- object t;
- using (MemoryStream ms = new MemoryStream(bytes, 0, bytes.Length))
- {
- t = Serializer.NonGeneric.Deserialize(type, ms);
- }
- ISupportInitialize iSupportInitialize = t as ISupportInitialize;
- if (iSupportInitialize == null)
- {
- return t;
- }
- iSupportInitialize.EndInit();
- return t;
- }
- public static object FromBytes(Type type, byte[] bytes, int index, int length)
- {
- object t;
- using (MemoryStream ms = new MemoryStream(bytes, index, length))
- {
- t = Serializer.NonGeneric.Deserialize(type, ms);
- }
- ISupportInitialize iSupportInitialize = t as ISupportInitialize;
- if (iSupportInitialize == null)
- {
- return t;
- }
- iSupportInitialize.EndInit();
- return t;
- }
- }
- }
|