2010-01-15 42 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