I have several message queues that have specific messages on them. I've created classes for these messages using xsd.exe. I can receive a message syncronously and deseriazlise it with this method:
public oneOfMyTypes DeserializeMessage(XDocument message)
{
 var serializer = new XmlSerializer(typeof(oneOfMyTypes));
 var entity = (oneOfMyTypes)serializer.Deserialize(message.CreateReader());
 return entity;
}
I then persist the entity via Fluent NHibernate.
So I've got about five message queues that all have their own type of message. I would like to keep this little processor app maintainable, so that adding more message queues and message types doesn't become a pain.
So I have a list of queue names in my app.config that I use to create the message queues on start up and then I want to wire up a single method to the .ReceiveCompleted event of all queues:
void queue_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
{
 var queue = (MessageQueue)sender;
 var message = queue.EndReceive(e.AsyncResult);
 var body = message.Body.ToString();
 var xml = XDocument.Parse(body);
 var queueName = queue.QueueName;
 Type entityType = GetTypeFromQueueName(queueName);
 entityType entity = DeserializeMessage<entityType>(xml);
 var repository = new LogRepository();
 repository.AddEntity<entityType>(entity);
}
private T DeserializeMessage<T>(XDocument message)
{
 var serializer = new XmlSerializer(typeof(T));
 var entity = (T)serializer.Deserialize(message.CreateReader());
 return entity;
}
public Type GetTypeFromQueueName(string queueName)
{
 switch (queueName)
 {
  case "some-message-queue-name":
   return typeof (oneOfMyTypes);
 }
}
But when I try to pass entityType to the generic methods I get "Type or namespace name expected". I'm probably doing something really silly, but I can't figure out how this should work.
I've tried using the dynamic keyword and also .MakeGenericType but no luck. I've also looked at:
- Dynamic Generic declaration of type T
- Function returning a generic type whose value is known only at runtime
- Determining a Generic Type at Runtime in Non-Generic Class
- How to pass variable of type "Type" to generic parameter
But I'm still not getting it ... help?
 
     
     
    