0

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!

Casey Crookston
  • 13,016
  • 24
  • 107
  • 193
  • Possible duplicate of [How do I use reflection to call a generic method?](http://stackoverflow.com/questions/232535/how-do-i-use-reflection-to-call-a-generic-method) – 31eee384 Jan 08 '16 at 16:06
  • There *are* ways to switch from reflection (type-based) code into static (generic-based) code, but it is rarely worth it; the most obvious solution here would be: don't use generics at this point. Do you need that? – Marc Gravell Jan 08 '16 at 16:08
  • Avoiding generics is exactly what I'm trying to figure out how to do! – Casey Crookston Jan 08 '16 at 16:09

1 Answers1

2

Obviously for such dynamic purposes where you don't know the type at compile-time you will need to provide an overload of your Deserialize method:

public static object Deserialize(Type targetType, string xml)
{
    ...
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • ok, this is making sense. I may start a new thread on how to write the overloaded method, if I need help with that. Thanks! – Casey Crookston Jan 08 '16 at 16:14
  • Sure, the implementation would depend on the specific XML serializer you are using but if you are using the standard `XmlSerializer` you could specify the target type when instantiating it: `var serializer = new XmlSerializer(targetType)`. – Darin Dimitrov Jan 08 '16 at 16:15
  • 1
    Wow, isn't passing a type as a normal parameter kind of defeating the purpose of Type Parameters? But this is probably Microsoft's fault, not yours. – NH. Jun 26 '17 at 16:30