tags:

views:

127

answers:

1

Is there any way to express this idea in C#? (Basically a generic type of a generic type)?

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  A: 

The generic constraint might not be quite what you want, but is this basically what you're looking for? This constrains TCollection to be an IEnumerable<T> (although IEnumerable can be swapped for 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();
}

Update: just changed my sample a little to better follow the method--I'd been confused and didn't realize you wanted a Where() method, I assumed it was your generic-constraint where.

STW
Yes, that will work (by it self) I played around with that signature. Unfortunately (probably for me) I'm trying to get near the same signature as the System.Linq.Enumerable.Where extension method (with a couple extra constraints - mainly the INotifyCollecitonChanged).
Jason Jarrett
OK, even though (before I posted on StatckOverflow) I tried the signature you gave me in the answer and couldn't get something with it to work... I was able to get it to work.Thanks anyway :)
Jason Jarrett
Maybe use `Where<TCollection, T>` instead of `Where<T, TCollection>` following the pattern of `Func<T1, TResult>` (generic parameters in order before generic result type).
280Z28