简体   繁体   中英

Obtain a collection of generic types from PagedCollectionView

Suppose I had a PageCollectionView of ItemType, where T is some user class that was defined in the project. My ItemType class has a property called IsSelected that I want to perform a query on. Effectively, this is what I want to do:

var objects = Source.SourceCollection.OfType<ItemType<>>().Where(t => t.IsSelected);

Of course this doesn't work because the ItemType generic needs a type. I can't specify the real type for the collection as I don't know it at this level (plus I don't want to hardcode the real type anyway, defeats the purpose of having the generic). Most likely, I could figure out a way using reflection, but I'd rather not for readability. Is there someway to do this?

Thanks!

Why not define an interface?

public interface ISelectable
{
   pubilc bool IsSelected { get; }
}

public class ItemType<T> : ISelectable
{
   ...
}

var objects = Source.SourceCollection
   .OfType<ISelectable>()
   .Where(t => t.IsSelected);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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