System.Reflection.AmbiguousMatchException:'发现模糊匹配。 [英] System.Reflection.AmbiguousMatchException: 'Ambiguous match found.'
问题描述
我正在尝试从方法 TableExists< T>
中获取 MethodInfo
,因此我可以使用
I am trying to get the MethodInfo
from a method TableExists<T>
so I can call it with a type.
该方法在 OrmLiteSchemaApi
类中声明。有2个重载:
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
}
我正在尝试获取 MethodInfo
像这样:
I am trying to get the MethodInfo
like this:
var tableMethod = typeof(OrmLiteSchemaApi).GetMethod("TableExists");
但它会生成异常:
System.Reflection.AmbiguousMatchException:'发现了模糊匹配。'
System.Reflection.AmbiguousMatchException: 'Ambiguous match found.'
我只能找到一个与建议将空对象数组作为参数传递,但这似乎不适用于.net核心。
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.
如何获取 MethodInfo
?
推荐答案
您可以使用 GetMethods
(复数!)以获取所有匹配方法的数组,然后查找具有 IsGeneri cMethod
:
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.
这篇关于System.Reflection.AmbiguousMatchException:'发现模糊匹配。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!