I have the following two functions:
public class MyClass
{
public void Save<TObject>(TObject object) where TObject : class
{
}
public void Save<TObject>(TObject object, String strValue) where TObject : class
{
}
}
I want to be able to dynamically call the first save function similar to the following:
public void DoSomething<T>(String strMethod) where T : class
{
T myObject = Activator.CreateInstance<T>();
MyClass.GetType().GetMethod(strMethod, new Type[] { typeof(T) }).MakeGenericMethod(typeof(T)).Invoke(null, new[] { myObject });
}
Unfortunately, when I do this, it is unable to match the first save function. If I remove the new Type[] { typeof(T) } I am stuck with an ambiguity issue. What am I missing?
The generic type arguments won’t match; the specific
Tin yourDoSomethingis not the same as the open parameterTObject. Instead, search for allSavemethods, and then filter after that: