Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't think .NET allows you to <em>create</em> a type "T" where T is a type argument, <strong>which is yet to be specified</strong>. So, the array of <code>Type</code>(s) from input string array cannot be created. </p> <p>However, in the second part of your question, I read that you want to <em>identify</em> the method which has those types given as string. That task is solvable by iterating though the arguments, creating another array of strings describing the method arguments, and then comparing the resulting and input arrays, as follows:</p> <pre><code> class MyClass&lt;T&gt; { public void MyMethod(T a, List&lt;T&gt; b, List&lt;Tuple&lt;T, string&gt;&gt; c) { } } class Program { static void Main(string[] args) { //input. var typesAsStr = new string[] { "T", "List`1[T]", "List`1[Tuple`2[T, string]]" }; //type to find a method. Type testType = typeof(MyClass&lt;&gt;); //possibly iterate through methods instead? MethodInfo myMethodInfo = testType.GetMethod("MyMethod"); //get array of strings describing MyMethod's arguments. string[] paramTypes = myMethodInfo.GetParameters().Select(pi =&gt; TypeToString(pi.ParameterType)).ToArray(); //compare arrays of strings (can be improved). var index = -1; Console.WriteLine("Method found: {0}", typesAsStr.All(str =&gt; { index++; return index &lt; paramTypes.Length &amp;&amp; str == paramTypes[index]; })); Console.ReadLine(); } private static CSharpCodeProvider compiler = new CSharpCodeProvider(); private static string TypeToString(Type type) { if (type.IsGenericType) { return type.Name + "[" + string.Join(", ", type.GetGenericArguments().Select(ga =&gt; TypeToString(ga))) + "]"; } else if (type.IsGenericParameter) { return type.Name; } //next line gives "string" (lower case for System.String). //additional type name translations can be applied if output is not what we neeed. return compiler.GetTypeOutput(new CodeTypeReference(type)); } } </code></pre> <p>In the [console] output I see that your input string <strong>matches</strong> the function.</p> <p>BTW, a lot of optimizations can be applied to this code if you face the performance problems, such as efficient way of working with strings, releasing <code>CSharpCodeProvider</code> instance maybe, etc. But the code is enough to solve the given task as questioned.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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