views:

678

answers:

1

I have two classes SccmAction and TicketAction which both implement interface IDelivery. These classes will be transmitted on a processing queue from which I just want to pull the message and act upon the Deliver method.

It seems however that I cannot deserialize to an interface because when I attempt to do so a System.NotSupportedException is thrown. From what I have gathered the XMLSerializer requires a concrete type for serialization. Does this leave me having to create an abstract class which implements IDelivery and inheriting SccmAction and ArsAction from it? Have I missed something that?

Edit Further Clarification

I may have a design flaw but my purpose is to pull messages off of a queue containing these objects. My intention was to have all objects coming off of this queue implement the interface ensuring that the application processing the objects just operates on the Deliver method. Each objects implementation of Deliver would vary.

Code to Deserialize

using (SqlConnection connection =
    new SqlConnection(ConnectionString))
{
    SqlCommand command = new SqlCommand(ReceiveString, connection);
    connection.Open();

    SqlDataReader reader = command.ExecuteReader();

    while (reader.Read())
    {
        byte[] sb = (byte[])reader["message_body"];
        SccmAction sa = DeserializeObject<SccmAction>(sb);
        IDelivery iD = DeserializeObject<IDelivery>(sb);
    }

    reader.Close();
}

public static T DeserializeObject<T>(byte[] ba)
{
    XmlSerializer xs = new XmlSerializer(typeof(T));
    MemoryStream memoryStream = new MemoryStream(ba);
    XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
    return (T)xs.Deserialize(memoryStream);
}

Classes and Interface

[Serializable]
public class SccmAction : IDelivery
{
    public string MachineName { get; set; }
    public string CollectionName { get; set; }
    public string Action { get; set; }
    public DateTime EntryDateTime { get; set; }

    public SccmAction () { }

    public void Deliver()
    {
        throw new NotImplementedException();
    }
}

[Serializable]
public class ArsAction : IDelivery
{
    public string MachineName { get; set; }
    public string CustomerName { get; set; }

    public ArsAction() { }

    public void Deliver()
    {
        throw new NotImplementedException();
    }
}

public interface IDelivery
{
    void Deliver();
}
+2  A: 

Simply put, you can't serialize an interface without any dirtiness. But why do you want to serialize your interface anyway, it doesn't hold any state. It only has a declaration for a method.

You might want to create a Serialize method inside your classes. So they can serialize themselves individually, they know which type they are.

Besides, how do you expect the XmlSerializer to Deserialize to an exact type without providing it? It can't simply pick what to Deserialize to....

You could however change this line

IDelivery iD = DeserializeObject<IDelivery>(sb);

to this

IDelivery iD = DeserializeObject<SccmAction>(sb);

If you dont know what type you are deserializing to initially (ArsAction or SccmAction), then you can do something like this.

 public static IDelivery DeserializeFromString(string xml)
{
//replace this stream with whatever you are useing
StringReader strReader = new StringReader(xml);
XmlReader reader = new XmlTextReader(fs); //important to use XmlReader
              reader.MoveToContent(); //move to root

              String className = reader.Name.Trim(); //read the class name 

              //use the namespace IDelivery is located in
              className  = "IDeliveryNamespace." + className;

              //get the type
              Type classType = Type.GetType(className);

              XmlSerializer serializer = new XmlSerializer(Type.GetType(className));      
              // Declare an object variable of the type to be deserialized.
              IDelivery i;



              // Use the Deserialize method to restore the object's state.
             i = (IDelivery)Convert.ChangeType(serializer.Deserialize(reader),classType);

            return i;
}

Of course this assumes that you your Serializable class Name is not changed. Meaning ArsAction class serializes to and as far as I can tell from what you posted that is the case.

This code is a little dirty, but it should give you a starting point.

Stan R.
edited to address your concerns
ahsteele
While IDelivery iD = DeserializeObject<SccmAction>(sb); will work I am trying to avoid having to know what type is serialized in the message.
ahsteele
@ashteele: i had a feeling you would say that. I edited my answer.
Stan R.
It feels a bit kludgy but it works! Thanks for the help.
ahsteele