How can I instantiate a generic container such as List<T> where T is specified in config in C# 4.0?-Collection of common programming errors
I have an application in which I would like to define message queues in config. So I would like to specify in config some number of message types such as “app.msg.UpdateMsg” or “app.msg.SnapshotMsg” for which to create queues.
Say my message queue class looks like this:
public class MsgQueue : where T: MsgBase, new()
{
private readonly Action _queueListener;
public MsgQueue(Action queueListener)
{
_queueListener = queueListener;
}
...
}
Now let’s say I have another class that wants to read from config the queue types listed there, and put them into a container. Something like this:
public class QueueManager
{
// We know T is a MsgBase, but not much else :(
private List _msgQueues = new List();
public QueueManager()
{
List configuredQueueTypes = GetQueueTypesFromConfig();
PopulateMsgQueues(configuredQueueTypes);
}
private void PopulateMsgQueues(List qTypes)
{
foreach (string qType in qTypes)
{
Action listener = GetListener(qType);
// What goes here? How do I create a MsgQueue?
}
}
...
}
How do I define PopulateMsgQueues(), if that’s even possible?
Is it possible (and would it help) if I could specify in config something like “app.MsgQueue of app.msg.UpdateMsg”?
Does anyone know of any other way to instantiate a bunch of MsgQueue of T where T is specified by a string at runtime?
I’m using C# 4.0, so can the dynamic keyword help me?
Thanks!