Akka.Interfaced.ProtobufSerializer.ProtobufSerializer.ToBinary C# (CSharp) Метод

ToBinary() публичный Метод

public ToBinary ( object obj ) : byte[]
obj object
Результат byte[]
        public override byte[] ToBinary(object obj)
        {
            // NotificationMessage

            var notificationMessage = obj as NotificationMessage;
            if (notificationMessage != null)
            {
                using (var ms = new MemoryStream())
                {
                    // write code, observerId and notificationId

                    ms.WriteByte((byte)MessageCode.Notification);
                    ms.Write7BitEncodedInt(notificationMessage.ObserverId);
                    ms.Write7BitEncodedInt(notificationMessage.NotificationId);

                    // write message

                    WriteType(ms, notificationMessage.InvokePayload.GetType());
                    try
                    {
                        AkkaSurrogate.CurrentSystem = system;
                        _typeModel.Serialize(ms, notificationMessage.InvokePayload);
                    }
                    finally
                    {
                        AkkaSurrogate.CurrentSystem = null;
                    }

                    return ms.ToArray();
                }
            }

            // RequestMessage

            var requestMessage = obj as RequestMessage;
            if (requestMessage != null)
            {
                using (var ms = new MemoryStream())
                {
                    // write code & requestId

                    ms.WriteByte((byte)MessageCode.Request);
                    ms.Write7BitEncodedInt(requestMessage.RequestId);

                    // write message

                    WriteType(ms, requestMessage.InvokePayload.GetType());
                    try
                    {
                        AkkaSurrogate.CurrentSystem = system;
                        _typeModel.Serialize(ms, requestMessage.InvokePayload);
                    }
                    finally
                    {
                        AkkaSurrogate.CurrentSystem = null;
                    }

                    return ms.ToArray();
                }
            }

            // ResponseMessage

            var responseMessage = obj as ResponseMessage;
            if (responseMessage != null)
            {
                using (var ms = new MemoryStream())
                {
                    if (responseMessage.Exception == null && responseMessage.ReturnPayload == null)
                    {
                        ms.WriteByte((byte)MessageCode.ReplyWithNothing);
                        ms.Write7BitEncodedInt(responseMessage.RequestId);
                    }
                    else if (responseMessage.Exception != null)
                    {
                        ms.WriteByte((byte)MessageCode.ReplyWithException);
                        ms.Write7BitEncodedInt(responseMessage.RequestId);

                        var exceptionType = responseMessage.Exception.GetType();
                        WriteType(ms, exceptionType);
                        if (_typeModel.CanSerialize(exceptionType))
                            _typeModel.Serialize(ms, responseMessage.Exception);
                    }
                    else
                    {
                        ms.WriteByte((byte)MessageCode.ReplyWithResult);
                        ms.Write7BitEncodedInt(responseMessage.RequestId);

                        // write result

                        WriteType(ms, responseMessage.ReturnPayload.GetType());
                        try
                        {
                            AkkaSurrogate.CurrentSystem = system;
                            _typeModel.Serialize(ms, responseMessage.ReturnPayload);
                        }
                        finally
                        {
                            AkkaSurrogate.CurrentSystem = null;
                        }
                    }
                    return ms.ToArray();
                }
            }

            throw new InvalidOperationException(
                "ProtobufSerializer supports only NotificationMessage, RequestMessage and ResponseMessage.");
        }