2017-05-26 125 views
0

有什麼方法可以使用約束來檢查方法參數是否實現多個接口?如何檢查方法參數是否實現多個接口

拿這個簡單的例子,用來檢查是否τ響應已經執行IBaseSearchResponse:

public static TResponse Search<TResponse, TRequest>(TRequest args) 
    where TResponse : IBaseSearchResponse {} 

,但我想也知道,如果它實現IBaseSearchProps。 香港專業教育學院試圖做這樣的事情添加約束:

public static TResponse Search<TResponse, TRequest>(TRequest args) 
    where TResponse : IBaseSearchArgs where TResponse : IBaseSearchProps {} 

但這個報告已經用於類型τ響應 一個限制條款,:

public static TResponse Search<TResponse, TRequest>(TRequest args) 
    where TResponse : (IBaseSearchArgs && IBaseSearchProps) {} 

這只是非法語法

我如果我的問題沒有準備好,在其他地方回答,或者如果答案在c#規範中定義,我會提前道歉...,我至少在這裏查看了一下

回答

6

你需要用逗號將它們分開(,):

例如

public static TResponse Search<TResponse, TRequest>(TRequest args) 
where TResponse : IBaseSearchArgs , IBaseSearchProps {...} 
+0

啊...不能更簡單或讓我感覺更愚蠢嗎...... – axa

2

這是一個逗號分隔的列表:

public void F<T>(T t) 
    where T : IEnumerable, IFooBar 
{ 
} 
相關問題