Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>EDIT: Okay, time for a short but complete program. The basic answer is as before:</p> <ul> <li>Find the "open" generic method with Type.GetMethod</li> <li>Make it generic using MakeGenericMethod</li> <li>Invoke it with Invoke</li> </ul> <p>Here's some sample code. Note that I changed the query expression to dot notation - there's no point in using a query expression when you've basically just got a where clause.</p> <pre><code>using System; using System.Linq; using System.Reflection; namespace Interfaces { interface IFoo {} interface IBar {} interface IBaz {} } public class Test { public static void CallMe&lt;T&gt;() { Console.WriteLine("typeof(T): {0}", typeof(T)); } static void Main() { MethodInfo method = typeof(Test).GetMethod("CallMe"); var types = typeof(Test).Assembly.GetTypes() .Where(t =&gt; t.Namespace == "Interfaces"); foreach (Type type in types) { MethodInfo genericMethod = method.MakeGenericMethod(type); genericMethod.Invoke(null, null); // No target, no arguments } } } </code></pre> <p><strong>Original answer</strong></p> <p>Let's leave aside the obvious problems of calling a variable "interface" to start with.</p> <p>You have to call it by reflection. The point of generics is to put more type checking at <em>compile</em> time. You don't know what the type is at compile-time - therefore you've got to use generics.</p> <p>Get the generic method, and call MakeGenericMethod on it, then invoke it.</p> <p>Is your interface type itself actually generic? I ask because you're calling MakeGenericType on it, but not passing in any type arguments... Are you trying to call</p> <pre><code>Method&lt;MyNamespace.Interface&lt;string&gt;&gt;(); // (Or whatever instead of string) </code></pre> <p>or </p> <pre><code>Method&lt;MyNamespace.Interface&gt;(); </code></pre> <p>If it's the latter, you only need a call to MakeGenericMethod - not MakeGenericType.</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