小编典典

具有多个约束的通用方法

c#

我有一个具有两个通用参数的通用方法。我试图编译下面的代码,但是它不起作用。是.NET的限制吗?是否可能对不同的参数具有多个约束?

public TResponse Call<TResponse, TRequest>(TRequest request)
  where TRequest : MyClass, TResponse : MyOtherClass

阅读 320

收藏
2020-05-19

共1个答案

小编典典

可以做到这一点,您只是语法有些错误。您需要where为每个约束而不是用逗号分隔约束:

public TResponse Call<TResponse, TRequest>(TRequest request)
    where TRequest : MyClass
    where TResponse : MyOtherClass
2020-05-19