Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate open constructed type from string
    text
    copied!<p>Let's say I have the following class.</p> <pre><code>MyClass&lt;T&gt; { public void MyMethod(T a, List&lt;T&gt; b, List&lt;Tuple&lt;T, string&gt;&gt; c) {} } </code></pre> <p>I can get the type of the arguments of the method as follow</p> <pre><code>Type testType = typeof(MyClass&lt;&gt;); MethodInfo myMethodInfo = testType.GetMethod("MyMethod"); Type[] paramTypes = myMethodInfo.GetParameters().Select(pi =&gt; pi.ParameterType); </code></pre> <p>How can I manually create an array containing the <strong>same open types</strong> as <code>paramTypes</code> from a string? For ex from</p> <pre><code>var typesAsStr = new string[] {"T", "List`1[T]", "List`1[Tuple`2[T, string]]"}; </code></pre> <p><strong>If</strong> I had <code>MyClass&lt;int&gt;</code>, I could do something like <code>Type.GetType(fullQualifiedNameOfArg)</code> for each argument, <strong>but</strong> here I want to keep the generic argument T:</p> <ul> <li>I can't create "a": I can't do <code>Type.GetType("T")</code></li> <li>I can almost create "b": I can do <code>Type.GetType("List</code> <code>`1")</code>, but the info on "T" is not yet present</li> <li>I don't know how to create "c"</li> </ul> <p>I ended up needing this when converting a Mono.Cecil type into a .net type: Cecil gives me the info on a method named <code>"MyMethod"</code> with arguments <code>"T"</code>, <code>"List&lt;T&gt;"</code> and <code>"List&lt;Tuple&lt;T, string&gt;&gt;"</code>. I then want to get that method using reflection (if there are several methods with the same name and argument numbers, I have to check the args to know which one it is), that's why I'd want to have a way to transform what Cecil tells me into what .Net knows, to be able to compare with what's in <code>paramTypes</code>.</p> <p>I've also seen several other people asking how to convert a Mono.Cecil type into a .Net one, so that's also why I thought I'd try.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload