I have a generic method with the following signature:
Broker.GetMessages<TType>();
It can be used in the following way:
IList<IEmailMessage> emails = Broker.GetMessages<IEmailMessage>();
I need to execute this method for a series of types available within an array of this structure:
var messageTypes = new [] { typeof(IEmailMessage), typeof(IFaxMessage) }
My final result should be something like this:
foreach ( IMessage message in messageTypes)
{
Broker.GetMessages<xxx>();
}
The problem is that I don't know how to covert the Type in order to pass it as a generic. I know I can use reflection to invoke the method but I was wondering if there is any better way to accomplish this. I can change the array structure but not the method signature.
Broker.GetMessages(Type messageType);. I bet you are callingtypeof(TType)inside that method, which indicates it shouldn't be generic at all.