gpt4 book ai didi

c# - System.Reflection.AmbiguousMatchException : 'Ambiguous match found.'

转载 作者:行者123 更新时间:2023-12-03 18:28:44 26 4
gpt4 key购买 nike

我正在尝试获取 MethodInfo来自方法 TableExists<T>所以我可以用一个类型来调用它。

该方法在 OrmLiteSchemaApi 中声明类(class)。有2个重载:

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像这样:
var tableMethod = typeof(OrmLiteSchemaApi).GetMethod("TableExists");

但它会产生异常:

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



我只能找到一个与此相关的旧问题,该问题建议将空对象数组作为参数传递,但这似乎不适用于 .net 核心。

我想我需要指定特定的重载,但我不确定具体如何。

我如何获得 MethodInfo ?

最佳答案

您可以使用 GetMethods (复数!)获取所有匹配方法的数组,然后查找具有 IsGenericMethod 的方法。 :

var tm = typeof(OrmLiteSchemaApi)
.GetMethods()
.Where(x => x.Name == "TableExists")
.FirstOrDefault(x => x.IsGenericMethod);

我建议这样做而不是使用参数说明符,因为它会给你一个对象,如果有任何问题,你可以在调试时逐步完成。

关于c# - System.Reflection.AmbiguousMatchException : 'Ambiguous match found.' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56083067/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com