C#(通用的通用)?

Jas*_*ett 4 c# generics

有没有办法在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();
    }
}
Run Code Online (Sandbox Code Playgroud)

STW*_*STW 5

通用约束可能不是你想要的,但这基本上是你想要的吗?这会将TCollection限制为IEnumerable<T>(尽管IEnumerable可以替换为List/Collection/Etc ......)

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

更新:刚刚改变我的样本以更好地遵循该方法 - 我一直困惑,并没有意识到你想要一个Where()方法,我认为这是你的通用约束where.