Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 114
  • Last Modified:

Determining the type to deserialize to

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
James Atkin
Asked:
James Atkin
1 Solution
 
AndyAinscowCommented:
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
 
James AtkinSenior Principle Software EngineerAuthor Commented:
Perfect, thanks :-)
0

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Tackle projects and never again get stuck behind a technical roadblock.
Join Now