Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Determining the type to deserialize to

Posted on 2014-09-19
2
Medium Priority
?
112 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:James Atkin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 2000 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:James Atkin
ID: 40332776
Perfect, thanks :-)
0

Featured Post

Create CentOS 7 Newton Packstack Running Keystone

A bug was filed against RDO for the installation of Keystone v3. This guide is designed to walk you through the configuration for using Keystone v3 with Packstack. You will accomplish this using various repos and the Answers file.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

722 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