c# - 声明一个带有两个泛型参数的方法

标签 c#

尝试执行以下操作:

public static class Qbo<T> where T : class
{
    public static List<Items> Convert(List<T> list1, List<T> list2)
    {
         // Merging lists of different types into one list
    }
}

这样调用:

var items = Qbo<Bill>.Convert(bills, billPayments);
var moreItems = Qbo<Invoice>.Convert(invoices, payments);

但是我得到一个错误,因为该方法试图接受第一个列表类型作为第二个列表类型。有什么想法吗?

最佳答案

通过您的编辑,您的需求会更清楚一些。您已经从类定义中获得了 T,但是您现在需要该方法的另一个泛型类型。所以也使方法调用通用:

public static class Qbo<T> where T : class
{
    public static List<Items> Convert<U>(List<T> list1, List<U> list2)
    //                               ^^^                    ^^^
    {
         // Merging lists of different types into one list
    }
}

关于c# - 声明一个带有两个泛型参数的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39133260/

相关文章:

c# - 以管理员身份运行 Powershell 命令 - 命令本身不会加载

c# - 在 OData 查询中使用复杂类型

c# - 在只有 2.0 框架的服务器上执行编译后的 3.5 代码时出现问题

c# - 初始化接口(interface)中的值

c# - IQueryable<T> 上的通用扩展方法

c# - 进度条从不显示

c# - 无法将类型为 'ASP.site_master' 的对象转换为类型 'System.Web.UI.WebControls.Button'。在 asp.net 中

c# - 获取 MailMessage 附件的路径

c# - 获取 IObservable<string> 对象作为对 POST 请求的响应,如何获取字符串本身?

C# DAL 返回泛型 List<T>