我有一个泛型方法,它有两个通用参数 . 我试图编译下面的代码,但它不起作用 . 它是.NET限制吗?是否可以为不同的参数设置多个约束?
public TResponse Call<TResponse, TRequest>(TRequest request) where TRequest : MyClass, TResponse : MyOtherClass
有可能这样做,你的语法略有错误 . 每个约束都需要where,而不是用逗号分隔它们:
public TResponse Call<TResponse, TRequest>(TRequest request) where TRequest : MyClass where TResponse : MyOtherClass
除了@LukeH的主要答案之外,我还有依赖注入的问题,我花了一些时间来解决这个问题 . 对于那些面临同样问题的人来说,值得分享一下:
public interface IBaseSupervisor<TEntity, TViewModel> where TEntity : class where TViewModel : class
它以这种方式解决了 . 在容器/服务中,键是typeof和逗号(,)
services.AddScoped(typeof(IBaseSupervisor<,>), typeof(BaseSupervisor<,>));
这在answer中提到过 .
2 回答
有可能这样做,你的语法略有错误 . 每个约束都需要where,而不是用逗号分隔它们:
除了@LukeH的主要答案之外,我还有依赖注入的问题,我花了一些时间来解决这个问题 . 对于那些面临同样问题的人来说,值得分享一下:
它以这种方式解决了 . 在容器/服务中,键是typeof和逗号(,)
这在answer中提到过 .