Solved

Determining the type to deserialize to

Posted on 2014-09-19
2
109 Views
Last Modified: 2014-09-19
Hi all,

I have two applications that send messages to one another using Names Pipes.
At one end, a class is serialized into a string using standard XML Serialization
[Serializable]
public class MyClassA
{
    public String s1;
    public String s2;
    public MyClassA()
    {
        s1 = "";
        s2 = "";
    }
}

Open in new window


To perform the Serialization and Deserialization, I am using the following
public static string SerializeObject<T>(this T toSerialize)
{
    string xmlstream = String.Empty;
    using (MemoryStream memstream = new MemoryStream())
    {
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
        XmlTextWriter xmlWriter = new XmlTextWriter(memstream, Encoding.UTF8);
        xmlSerializer.Serialize(xmlWriter, toSerialize);
        xmlstream = UTF8ByteArrayToString(((MemoryStream)xmlWriter.BaseStream).ToArray());
    }
    return xmlstream;
}

public static T DeSerializeObject<T>(String toDeSerialize)
{
    T tempObject = default(T);
    using (MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(toDeSerialize)))
    {
        XmlSerializer xs = new XmlSerializer(typeof(T));
        XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
        tempObject = (T)xs.Deserialize(memoryStream);
    }
    return tempObject;           
}

public static String UTF8ByteArrayToString(Byte[] ArrBytes) { return new UTF8Encoding().GetString(ArrBytes); }
public static Byte[] StringToUTF8ByteArray(String XmlString) { return new UTF8Encoding().GetBytes(XmlString); }

Open in new window


When I receive the message, I decode it using:
MyClassA cls = DeSerializeObject<MyClassA>(message_string);

Open in new window

and it works great.

Now, if I receive a message that is a different class, how can I determine this before attempting to Deserialize it?

Any help would be appreciated

James
0
Comment
Question by:jatkin
2 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 40332760
Assuming you are passing the XML string between the two apps via the pipe you could prepend a string with the class type.

eg.
Sending:
string s = "MyClassA;" + xmlString;
or
string s = "AnotherClassB;" + xmlString;


and receiving you strip of the first part up to the ';' and then
if(sTag == "MyClassA")
  deserialise_classA;
else if(sTag=="AnotherClassB")
  deserialise_classB
and so on
0
 
LVL 4

Author Closing Comment

by:jatkin
ID: 40332776
Perfect, thanks :-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question