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?