I have a method that looks like this:
private IEnumerable<T> QueryCollection<T>() where T : BaseObj
{
IEnumerable<T> items = query<T>();
return items;
}
I now have a situation where I want to filter this items collection IF the "T" supports a certain interface (it might not so i can't simply add it as a constraint of T). So i want something like this:
private IEnumerable<T> QueryCollection<T>() where T : BaseObj
{
IEnumerable<T> items = query<T>();
if (typeOf(T).GetInterface(ITeamFilterable) != null)
{
items = FilterByTeams(items);
}
return items;
}
What is the recommended way of checking if my generic Type supports a certain interface **and then if YES, then
- Use that within the method to filter a collection
- But still return the collection at type "T" in the overall method
NOTE: FilterByTeams takes in an:
IEnumerable<ITeamFilterable>
AND returns
IEnumerable<ITeamFilterable>
Do I need to cast the collection 2 times (one to convert to List of the interface and then again to convert back to list of T?)