133

Edit:

Of course my real code doesn't look exactly like this. I tried to write semi-pseudo code to make it more clear of whay I wanted to do.

Looks like it just messed things up instead.

So, what I actually would like to do is this:

Method<Interface1>();
Method<Interface2>();
Method<Interface3>();
...

Well ... I thought that maybe I could turn it into a loop using reflection. So the question is: How how do I do it. I have very shallow knowledge of reflection. So code examples would be great.

The scenario looks like this:

public void Method<T>() where T : class
{}
public void AnotherMethod()
{
    Assembly assembly = Assembly.GetExecutingAssembly();

    var interfaces = from i in assembly.GetTypes()
    where i.Namespace == "MyNamespace.Interface" // only interfaces stored here
    select i;

    foreach(var i in interfaces)
    {
        Method<i>(); // Get compile error here!
    }




Original post:

Hi!

I'm trying to loop through all interfaces in a namespace and send them as arguments to a generic method like this:

public void Method<T>() where T : class
{}
public void AnotherMethod()
{
    Assembly assembly = Assembly.GetExecutingAssembly();

    var interfaces = from i in assembly.GetTypes()
    where i.Namespace == "MyNamespace.Interface" // only interfaces stored here
    select i;

    foreach(var interface in interfaces)
    {
        Method<interface>(); // Get compile error here!
    }
}

The error I get is "Type name expected, but local variable name found". If I try

...
    foreach(var interface in interfaces)
    {
        Method<interface.MakeGenericType()>(); // Still get compile error here!
    }
}

I get "Cannot apply operator '<' to operands of type 'method group' and 'System.Type'" Any idea on how to get around this problem?

M4N
  • 94,805
  • 45
  • 217
  • 260
Presidenten
  • 6,327
  • 11
  • 45
  • 55

1 Answers1

159

EDIT: Okay, time for a short but complete program. The basic answer is as before:

  • Find the "open" generic method with Type.GetMethod
  • Make it generic using MakeGenericMethod
  • Invoke it with Invoke

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.

using System;
using System.Linq;
using System.Reflection;

namespace Interfaces
{
    interface IFoo {}
    interface IBar {}
    interface IBaz {}
}

public class Test
{
    public static void CallMe<T>()
    {
        Console.WriteLine("typeof(T): {0}", typeof(T));
    }

    static void Main()
    {
        MethodInfo method = typeof(Test).GetMethod("CallMe");

        var types = typeof(Test).Assembly.GetTypes()
                                .Where(t => t.Namespace == "Interfaces");

        foreach (Type type in types)
        {
            MethodInfo genericMethod = method.MakeGenericMethod(type);
            genericMethod.Invoke(null, null); // No target, no arguments
        }
    }
}

Original answer

Let's leave aside the obvious problems of calling a variable "interface" to start with.

You have to call it by reflection. The point of generics is to put more type checking at compile time. You don't know what the type is at compile-time - therefore you've got to use generics.

Get the generic method, and call MakeGenericMethod on it, then invoke it.

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

Method<MyNamespace.Interface<string>>(); // (Or whatever instead of string)

or

Method<MyNamespace.Interface>();

If it's the latter, you only need a call to MakeGenericMethod - not MakeGenericType.

huysentruitw
  • 27,376
  • 9
  • 90
  • 133
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I would like to stub the generic method, based on the type. But if I Make it using reflection, and then stub the generated one, it understand it won't stub what I want. My guess is that it's impossible? Or is it? Stub the generic method for all the types in a list... – Stéphane Dec 09 '10 at 10:41
  • 1
    @Stephane: It's hard to know exactly what you're trying to do. I suggest you ask a new question with more details. – Jon Skeet Dec 09 '10 at 11:07
  • Note you may get "Ambiguous matching in method resolution" on `GetMethod()`. This happens when the method you trying to get has some overloads. So you need to specify which one you want by using `GetMethod("Name", new Type[] {arguments})` – cvsguimaraes Jan 11 '13 at 11:51