System.Reflection.AmbiguousMatchException: '找到了不明确的匹配.' [英] System.Reflection.AmbiguousMatchException: 'Ambiguous match found.'
问题描述
我正在尝试从方法 TableExists
获取 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
(复数!)获取所有匹配方法的数组,然后查找具有 IsGenericMethod代码>
:
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屋!