我有一個聲明,像這樣的功能:函數聲明
public static string MultiWhereToString(List<WhereCondition<T>> whereConditions)
我想傳遞一個叫whereAnd
變量,它是delcared像這樣:
private List<WhereAndCondition<T>> whereAnd = new List<WhereAndCondition<T>>();
WhereAndCondition
是WhereCondition
的子類。它宣稱,像這樣:
public class WhereAndCondition<T> : WhereCondition<T>, IConditional where T : DatabaseObject
我的問題是,如果我嘗試執行以下代碼:
private List<WhereAndCondition<T>> whereAnd = new List<WhereAndCondition<T>>();
MultiWhereToString(whereAnd);
我收到以下錯誤:
Error 3 Argument 1: cannot convert from 'System.Collections.Generic.List<BrainStorm.WhereAndCondition<T>>' to 'System.Collections.Generic.List<BrainStorm.WhereCondition<T>>'
爲什麼任何想法?我認爲它與WhereCondition
類的泛型有關。
你可以'公共靜態字符串MultiWhereToString(IList> whereConditions)'? –
Joe
[在C#中,是否可以將列表轉換爲列表?](http://stackoverflow.com/questions/1777800/in-c-is-it-possible-to-cast-a-listchild-to -listparent) –
如果您要傳遞IEnumerable而不是List,則可以利用協方差特徵。 http://blogs.msdn.com/b/csharpfaq/archive/2010/02/16/covariance-and-contravariance-faq.aspx – spender