axa*_*axa 0 c# interface constraints
有没有办法使用约束来检查方法参数是否实现了多个接口?
以这个简单的例子来检查TResponse是否已经实现了IBaseSearchResponse:
public static TResponse Search<TResponse, TRequest>(TRequest args)
where TResponse : IBaseSearchResponse {}
Run Code Online (Sandbox Code Playgroud)
但我想知道它是否实现了IBaseSearchProps.我曾试图通过以下方式添加约束:
public static TResponse Search<TResponse, TRequest>(TRequest args)
where TResponse : IBaseSearchArgs where TResponse : IBaseSearchProps {}
Run Code Online (Sandbox Code Playgroud)
但是这会报告已经用于类型TResponse的约束条款:
public static TResponse Search<TResponse, TRequest>(TRequest args)
where TResponse : (IBaseSearchArgs && IBaseSearchProps) {}
Run Code Online (Sandbox Code Playgroud)
这只是非法语法
如果我的问题准备不充分,在其他地方回答或者如果答案在c#规范中定义,我事先道歉,我至少在这里看一下
你需要用逗号(,)分隔它们:
例如
public static TResponse Search<TResponse, TRequest>(TRequest args)
where TResponse : IBaseSearchArgs , IBaseSearchProps {...}
Run Code Online (Sandbox Code Playgroud)