views:

250

answers:

2

Consider type like this one

public interface IHaveGenericMethod
{
   T1 Method<T1>(T1 parm);
   T2 Method<T1,T2>(T1 parm);
   int Method2(int parm);
}

How do I get a methodInfo for its methods? for a regular non-generic method, like method2, I can go with

typeof(IHaveGenericMethod).GetMethod("methodName",new Type[]{typeof(itsParameters)});

for a generic method though, I can't, since it's parameters are not types per-se. So, how do I do that? I know that I can call

typeof(IHaveGenericMethod).GetMethods()

to get all methods of that type, and then iterate over that collection and do some matching, but it's ugly. Is there a better way?

+1  A: 

Well, they are types - of sorts:

    foreach (var method in typeof(IHaveGenericMethod).GetMethods())
    {
        Console.WriteLine(method.Name);
        if (method.IsGenericMethodDefinition)
        {
            foreach (Type type in method.GetGenericArguments())
            {
                Console.WriteLine("> " + type.Name);
            }
        }
    }

So you can check by the number of args, and check the signature. But nothing cleaner.

Marc Gravell
well, yeah, but that does not answer my question
Krzysztof Koźmic
(replied to original question so it appears on your list...)
Marc Gravell
Fair enough .
Krzysztof Koźmic
+1  A: 

Be sure to check out the MSDN page "Reflection and Generic Types".

since it's parameters are not types per-se

Actually, I think it's because you want to query type parameters, but the type list you can provide to GetMethod() is not for type parameters.

Also, remember that all you need to select a "method group" of generic methods is to know the number of generic type parameters. So you can just count them.

then iterate

Don't iterate, query:

       var mi = from mi in typeof(IHaveGenericMethod).GetMethods()
                where mi.Name == "Method"
                where mi.IsGenericMethodDefinition
                where mi.GetGenericArguments().Length == 2
                select mi;
Jay Bazuzi
this is sneaky, but it still can return more than one method. Also it requires LINQ, ergo .NET 3.5, which is what I wanted to avoid.The only improvement I could think of is, instead of typeof(IHaveGenericMethod).GetMethods()use typeof(IHaveGenericMethod).GetMembers(lots,of,arguments)
Krzysztof Koźmic