繁体   English   中英

C#(通用的通用)?

[英]C# (Generic of a Generic)?

有没有办法在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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM