2010-01-15 56 views
4

有沒有什麼方法可以在C#中表達這個想法? (基本上泛型類型的泛型)?C#(通用泛型)?

public static class ExtensionSpike 
{ 
    public static IEnumerable<T> Where<TCollection<T>>(this TCollection<T> sourceCollection, Expression<Func<T, bool>> expr) 
     where TCollection : class, IEnumerable<T>, INotifyCollectionChanged 
    { 
     throw new NotImplementedException(); 
    } 
} 

回答

5

通用約束可能不完全是你想要的,但這基本上是你在找什麼?這限制TCollection是一個IEnumerable<T>(儘管IEnumerable的可調換的列表/收藏/等)

public static IEnumerable<T> Where<T, TCollection>(this TCollection sourceCollection, Expression<Func<T, bool>> expr) 
where TCollection : IEnumerable<T>, INotifyPropertyChanged 
{ 
    throw new NotImplementedException(); 
} 

更新:只是改變了我的樣品一點,以更好地遵循方法 - 我倒是一直困惑,並沒有意識到你想要一個Where()方法,我認爲這是你的通用約束where

+0

是的,這將工作(通過它自己)我玩了那個簽名。不幸的是(可能對我來說)我試圖接近與System.Linq.Enumerable.Where擴展方法相同的簽名(帶有一些額外的約束 - 主要是INotifyCollecitonChanged)。 – 2010-01-15 02:46:13

+0

好的,即使(在我發佈到StatckOverflow之前)我嘗試了你在答案中給予我的簽名,並且無法獲得與它一起工作的內容......我能夠使其正常工作。 謝謝:) – 2010-01-15 04:23:19

+1

可能使用'Where '而不是'Where '遵循'Func '的模式(在通用結果類型之前的通用參數)。 – 2010-01-15 17:55:49