In C#, I have a function that passes in T
using generics
and I want to run a check to see if T
is an object
that implements a
The missing piece is Cast<>()
:
if(typeof(IFilterable).IsAssignableFrom(typeof(T))) {
entities = FilterMe(entities.Cast()).AsQueryable().Cast();
}
Note the use of Cast<>()
to convert the entities list to the correct subtype. This cast would fail unless T
implements IFilterable
, but since we already checked that, we know that it will.