I've got a simple method that deserializes an xml string to a type that is specified.
public static class TextUtility
{
public static T Deserialize<T>(string xml)
{
...
}
}
That's all good. Where I'm having trouble is in calling this method. The process looks like this:
public interface IMessageReceived
{
string MessageBodyAsXML { get; set; }
string MessageType { get; set; }
}
public class MessageReceived : IMessageReceived
{...}
List<MessageReceived> messageList = new List<MessageReceived>();
Then, in the worker class that is processing the messages, we loop over all the messages received. Inside that loop, we loop over a list of potential message types.
foreach (MessageReceived message in messageList)
{
foreach (var processor in processors)
{
if (processor.TypeToProcess.Name == message.MessageType)
{
// Now Create an instance of 'processor' from message.MessageBodyAsXML
}
}
}
I tries this:
object messageObject = TextUtility.DeserializeMessage<object>(message.MessageBodyAsXML);
And it kinda works, but the generic 'object' causes problems down stream. I need to be more tightly coupled. I can do this:
Type type = processor.GetType();
And that does populate 'type' with the correct type. But then obviously I can't do this:
type messageObject = TextUtility.DeserializeMessage<type>(message.MessageBodyAsXML);
I am trying hard to avoid a long if/then for each type. Open to ideas. Thanks!