Gwupe.Cloud.Messaging.WebSocketMessageHandler.ParseDataToMessage C# (CSharp) Method

ParseDataToMessage() private method

private ParseDataToMessage ( MemoryStream data ) : Message
data System.IO.MemoryStream
return Gwupe.Cloud.Messaging.API.Message
        private Message ParseDataToMessage(MemoryStream data)
        {
            DataContractJsonSerializer detectSerializer = new DataContractJsonSerializer(typeof (MessageImpl));
            MemoryStream copyData = new MemoryStream();
            data.CopyTo(copyData);
            data.Position = copyData.Position = 0;
            MessageImpl messageDetect = (MessageImpl) detectSerializer.ReadObject(copyData);
            String[] typeInfo = messageDetect.type.Split('-');
            String className = "";
            if (typeInfo[1].Equals("RQ"))
            {
                className = "Gwupe.Cloud.Messaging.Request." + typeInfo[0] + "Rq";
            }
            else if (typeInfo[1].Equals("RS"))
            {
                className = "Gwupe.Cloud.Messaging.Response." + typeInfo[0] + "Rs";
            }
            else
            {
                throw new ProtocolException("Message type not recognised : " + messageDetect.type);
            }
            try
            {
                Type type = Type.GetType(className);
                DataContractJsonSerializer messageSerializer = new DataContractJsonSerializer(type);
                Message message = (Message) messageSerializer.ReadObject(data);
                return message;
            }
            catch (Exception e)
            {
                throw new ProtocolException("Failed to deserialize message : " + e.Message);
            }
        }