0

Say I have a class like this:

public class MyTestClass<T>
{

    public void DoSomething(object o)
    {
        Logger.Debug("Non-generic version called.");
    }

    public void DoSomething(T o)
    {
        Logger.Debug("Generic version called.");
    }

    public void DoOtherThing(object o)
    {  
        Logger.Debug("DoOtherThing called.");
    }
}

If I create an instance with type:MyTestClass<Object>, I can get "DoOtherThing" method use following code:

var t = typeof(MyTestClass<object>);
var doOtherThingMethod = t.GetMethod("DoOtherThing");

but there are two method named "DoSomeThing",how could I call its generic version using reflection?

To describe my question clearly,I give the following example.

public void TestGenericClass()
{
    var o = new MyTestClass<object>();
    var t = o.GetType();

    var methodInfos = t.GetMethods();

    //GetMethod will throw System.Reflection.AmbiguousMatchException here: Ambiguous matching in method resolution.
    var m = t.GetMethod("DoSomething");

    //there are two MethodInfo in methods.
    var methods = t.GetMethods("DoSomething");

    var m1 = methods[0];
    var m2 = methods[1];

}

The question is, which one is the method using generic type, m1 or m2?

lingtianlan
  • 304
  • 2
  • 7
  • It's not the same question with [How to use reflection to call generic Method](http://stackoverflow.com/questions/232535/how-to-use-reflection-to-call-generic-method) – lingtianlan Jun 29 '15 at 08:01
  • This one should be more similar. – xanatos Jun 29 '15 at 08:03
  • thank you for your comments, but I still think it is not a duplicated question. I updated the question and give an example, may be it is more clearly – lingtianlan Jul 03 '15 at 03:17

0 Answers0