Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Reflection.AmbiguousMatchException: 'Ambiguous match found.'

Tags:

c#

.net-core

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?

like image 615
Guerrilla Avatar asked May 10 '19 18:05

Guerrilla


2 Answers

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")
        .FirstOrDefault(x => x.IsGenericMethod);

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.

like image 189
David Avatar answered Oct 24 '22 04:10

David


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.

like image 35
Louis Ingenthron Avatar answered Oct 24 '22 02:10

Louis Ingenthron