HaoYueTunnel/ClientCore/Common/ProtoBufHelper.cs

23 lines
467 B
C#
Raw Permalink Normal View History

2023-05-24 18:31:11 +08:00
using Google.Protobuf;
2023-05-23 17:57:24 +08:00
2023-05-25 18:30:22 +08:00
namespace ClientCore.Common
2023-05-23 17:57:24 +08:00
{
2023-05-25 18:30:22 +08:00
public static class ProtoBufHelper
2023-05-23 17:57:24 +08:00
{
2023-05-24 18:31:11 +08:00
public static byte[] Serizlize(IMessage msg)
2023-05-23 17:57:24 +08:00
{
2023-05-24 18:31:11 +08:00
return msg.ToByteArray();
2023-05-23 17:57:24 +08:00
}
2023-05-24 18:31:11 +08:00
public static T DeSerizlize<T>(byte[] bytes)
2023-05-23 17:57:24 +08:00
{
2023-05-24 18:31:11 +08:00
var msgType = typeof(T);
object msg = Activator.CreateInstance(msgType);
((IMessage)msg).MergeFrom(bytes);
return (T)msg;
2023-05-23 17:57:24 +08:00
}
}
}