<p>I'm programming WCF using the ChannelFactory which expects a type in order to call the CreateChannel method. For example: </p>

<pre><code>IProxy proxy = ChannelFactory&lt;IProxy&gt;.CreateChannel(...);
</code></pre>

<p>In my case I'm doing routing so I don't know what type my channel factory will be using. I can parse a message header to determine the type but I hit a brick wall there because even if I have an instance of Type I can't pass that where ChannelFactory expects a generic type. </p>

<p>Another way of restating this problem in very simple terms would be that I'm attempting to do something like this:</p>

<pre><code>string listtype = Console.ReadLine(); // say "System.Int32"
Type t = Type.GetType( listtype);
List&lt;t&gt; myIntegers = new List&lt;&gt;(); // does not compile, expects a "type"
List&lt;typeof(t)&gt; myIntegers = new List&lt;typeof(t)&gt;(); // interesting - type must resolve at compile time?
</code></pre>

<p>Is there an approach to this I can leverage within C#?</p>

[{'answer_id': 67530, 'author': 'IDisposable', 'author_id': 2076, 'author_profile': 'https://Stackoverflow.com/users/2076', 'pm_score': 6, 'selected': True, 'text': '<p>What you are looking for is MakeGenericType</p>\n\n<pre><code>string elementTypeName = Console.ReadLine();\nType elementType = Type.GetType(elementTypeName);\nType[] types = new Type[] { elementType };\n\nType listType = typeof(List&lt;&gt;);\nType genericType = listType.MakeGenericType(types);\nIProxy  proxy = (IProxy)Activator.CreateInstance(genericType);\n</code></pre>\n\n<p>So what you are doing is getting the type-definition of the generic "template" class, then  building a specialization of the type using your runtime-driving types.</p>\n'}, {'answer_id': 67619, 'author': 'tomasr', 'author_id': 10292, 'author_profile': 'https://Stackoverflow.com/users/10292', 'pm_score': 2, 'selected': False, 'text': "<p>Here's a question: Do you <i>really</i> need to create a channel with the exact contract type in your specific case?</p>\n\n<p>Since you're doing routing, there's a very good chance you could simply deal with the generic channel shapes. For example, if you're routing a one-way only message, then you could create a channel to send the message out like this:</p>\n\n<pre><code>ChannelFactory&lt;IOutputChannel&gt; factory = new ChannelFactory&lt;IOutputChannel&gt;(binding, endpoint);\nIOutputChannel channel = factory.CreateChannel();\n...\nchannel.SendMessage(myRawMessage);\n</code></pre>\n\n<p>If you needed to send to a two-way service, just use IRequestChannel instead.</p>\n\n<p>If you're doing routing, it is, in general, a lot easier to just deal with generic channel shapes (with a generic catch-all service contract to the outside) and just make sure the message you're sending has all the right headers and properties.</p>\n"}, {'answer_id': 67782, 'author': 'Bill', 'author_id': 9887, 'author_profile': 'https://Stackoverflow.com/users/9887', 'pm_score': 3, 'selected': False, 'text': '<p>You should look at this post from Ayende: <a href="http://www.ayende.com/Blog/archive/2007/02/10/WCF-Mocking-and-IoC-Oh-MY.aspx" rel="noreferrer">WCF, Mocking and IoC: Oh MY!</a>.  Somewhere near the bottom is a method called GetCreationDelegate which should help.  It basically does this:</p>\n\n<pre><code>string typeName = ...;\nType proxyType = Type.GetType(typeName);\n\nType type = typeof (ChannelFactory&lt;&gt;).MakeGenericType(proxyType);\n\nobject target = Activator.CreateInstance(type);\n\nMethodInfo methodInfo = type.GetMethod("CreateChannel", new Type[] {});\n\nreturn methodInfo.Invoke(target, new object[0]);\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/67370', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/64/']