{"id":3775,"date":"2014-03-30T05:29:46","date_gmt":"2014-03-30T05:29:46","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-can-i-instantiate-a-generic-container-such-as-listt-where-t-is-specified-in-config-in-c-4-0-collection-of-common-programming-errors\/"},"modified":"2014-03-30T05:29:46","modified_gmt":"2014-03-30T05:29:46","slug":"how-can-i-instantiate-a-generic-container-such-as-listt-where-t-is-specified-in-config-in-c-4-0-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-can-i-instantiate-a-generic-container-such-as-listt-where-t-is-specified-in-config-in-c-4-0-collection-of-common-programming-errors\/","title":{"rendered":"How can I instantiate a generic container such as List&lt;T&gt; where T is specified in config in C# 4.0?-Collection of common programming errors"},"content":{"rendered":"<p>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 &#8220;app.msg.UpdateMsg&#8221; or &#8220;app.msg.SnapshotMsg&#8221; for which to create queues.<\/p>\n<p>Say my message queue class looks like this:<\/p>\n<pre><code>public class MsgQueue : where T: MsgBase, new()\n{\n    private readonly Action _queueListener;\n\n    public MsgQueue(Action queueListener)\n    {\n        _queueListener = queueListener;\n    }\n    ...\n}\n<\/code><\/pre>\n<p>Now let&#8217;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:<\/p>\n<pre><code>public class QueueManager\n{\n    \/\/ We know T is a MsgBase, but not much else :(\n    private List _msgQueues = new List();\n\n    public QueueManager()\n    {\n        List configuredQueueTypes = GetQueueTypesFromConfig();\n\n        PopulateMsgQueues(configuredQueueTypes);\n    }\n\n    private void PopulateMsgQueues(List qTypes)\n    {\n        foreach (string qType in qTypes)\n        {\n            Action listener = GetListener(qType);\n\n            \/\/ What goes here? How do I create a MsgQueue?\n        }\n    }\n    ...\n}\n<\/code><\/pre>\n<p>How do I define PopulateMsgQueues(), if that&#8217;s even possible?<\/p>\n<p>Is it possible (and would it help) if I could specify in config something like &#8220;app.MsgQueue of app.msg.UpdateMsg&#8221;?<\/p>\n<p>Does anyone know of any other way to instantiate a bunch of MsgQueue of T where T is specified by a string at runtime?<\/p>\n<p>I&#8217;m using C# 4.0, so can the dynamic keyword help me?<\/p>\n<p>Thanks!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &#8220;app.msg.UpdateMsg&#8221; or &#8220;app.msg.SnapshotMsg&#8221; for which to create queues. Say my message queue class looks like this: public class MsgQueue : where T: MsgBase, new() { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3775","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3775","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3775"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3775\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}