有什么办法可以用 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();
}
}
最佳答案
通用约束可能不是您想要的,但这基本上就是您要寻找的吗?这将 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();
}
更新:只是稍微更改了我的示例以更好地遵循该方法——我一直很困惑,没有意识到你想要一个 Where()
方法,我认为这是你的通用约束 where
.
关于C#(泛型的泛型)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2069043/