confused by generic methods

Hi,

in the code sample that follows how do i get from the MethodInfo object given the variable name 'method' in main (this method takes a string as its parameter) to the MethodInfo object that took the generic parameter in the class MyGeneric (i.e. the MethodINfo that originally described the method) I would guess by using MethodInfo.GetGenericMethodDefintion() but is says this isn't allowed due top the state of the object. I think i would be nearer to understanding my problem if i knew why IsGenericMethod is returning false below when i think it should be true... (IsGenericMethodDefiniton is also false).

thanks in advance, colin

using System;

using System.Reflection;

using System.Collections.Generic;

namespace AnotherTest {

public class MyGeneric<K>{

public bool ContainsKey(K key) {

return false;

}

}

class MainClass{

public static void Main(String[] args){

Type testType = typeof(MyGeneric<String>);

Console.WriteLine("is generic type " + testType.IsGenericType);

MethodInfo method = testType.GetMethods()[0];

Console.WriteLine("is generic method " + method.IsGenericMethod);

Console.Read();

}

}

}



Answer this question

confused by generic methods

  • scokim

    The method isn't generic.

    The following class contains a generic method.



    public class ContainsGenericMethod
    {
    void GenericMethod<T>(T value)
    {
    }
    }


  • Sentme

    actually, i'm not so sure i can...

    I can get the GenericTypeDefinition (MyGeneric in the example) and then i could use GetMethod(name, parameters) to get the original MethodInfo - if i knew which parameters to use. In other words how do i get back the signature for the method in the open generic

    I can get the generic arguments of the open generic and can get the generic arguments of the closed generic, I could then swap the types over. e.g. swap type String for type K in my original example.

    However, i cannot assume that all type String parameters in a method should be swapped for type K parameters. How do i know whether or not a parameters Type was changed as a result of binding the open generic to certain types

    I hope somebody has followed that :)

    col


  • Mr_Mod

    Hi, yes - thanks for the response.

    So how do you describe the relationship between the method I'm reflecting over ('MemberInfo method') and the method in the class i called MyGeneric

    Clearly they are related - if i add

    foreach (ParameterInfo param in method.GetParameters()) {

    Console.WriteLine(method.Name + "\t" + param.Name + "\t" + param.ParameterType);

    }

    to the end of the main method i can see the parameter has changed to a String.

    How do i get back to the MethodInfo that orignally described it

    cheers,

    colin


  • Dinesh Kumar M.G

    You can problably use the methods and properties described on this page: Generics and Reflection (C# Programming Guide) .


  • Peter S Antal - MSFT

    OK, i thought there might be an easier way of getting to the info.
  • confused by generic methods