Reputation: 14926
I am trying to get the MethodInfo
from a method TableExists<T>
so I can call it with a type.
The method is declared inside OrmLiteSchemaApi
class. There are 2 overloads:
public static bool TableExists<T>(this IDbConnection dbConn)
{
// code omitted
}
public static bool TableExists(this IDbConnection dbConn, string tableName, string schema = null)
{
// code omitted
}
I am trying to get the MethodInfo
like this:
var tableMethod = typeof(OrmLiteSchemaApi).GetMethod("TableExists");
But it generates exception:
System.Reflection.AmbiguousMatchException: 'Ambiguous match found.'
I could only find an old question related to this that suggested to pass an empty object array as parameter but this doesn't seem to work for .net core.
I guess I need to specify the specific overload but I am not sure exactly how.
How do I get the MethodInfo
?
Upvotes: 26
Views: 39467
Reputation: 10718
You can use GetMethods
(plural!) to get an array of all matching methods, and then look for the one which has IsGenericMethod
:
var tm = typeof(OrmLiteSchemaApi)
.GetMethods()
.Where(x => x.Name == "TableExists")
.Where(x => x.IsGenericMethod);
.FirstOrDefault();
I recommend this over using parameter specifiers, since it'll give you an object you can step through at debug time if there are ever any problems.
Upvotes: 34
Reputation: 1368
Passing an empty object array would only work if you're looking for a function with no parameters. Instead, you need to use a different overload of GetMethod that specifies the types of parameters as a type array. That way you can tell it which reference to get by specifying which types of parameters it should look for.
Upvotes: 7