Solved

Determining the type to deserialize to

Posted on 2014-09-19
2
100 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
Comment Utility
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
Comment Utility
Perfect, thanks :-)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now