I am facing the following problem with conversion of the inner type in Predicate<T>
. I have a class implementing a basic interface. Additionally to the interface, the class has some advanced properties and methods.
public interface IBasic { /* ... */ }
public class Advanced : IBasic { /* ... */ }
In a method I get a number of predicates over the Advanced
class and I would like to sort them into two lists: 1) predicates that use only the IBasic
interface 2) predicates that need all features from the Advanced
class.
List<Predicate<IBasic>> basic = new List<Predicate<IBasic>>();
List<Predicate<Advanced>> advanced = new List<Predicate<Advanced>>();
public void SetPredicates(params Predicate<Advanced>[] predicates)
{
foreach(var item in predicates)
{
//Of course the `is` keyword does not work here, always returning false.
//Is it possible to do the check on the lambda function in item in a different way?
if (item is Predicate<IBasic>)
basic.Add((Predicate<IBasic>)item); //And this cast throws an exception of course.
else
advanced.Add(item);
}
}
Questions
- Is it possible at all?
- How can I check if the generic type of the predicate can be reduced to the
IBasic
interface? - How can I perform the predicate cast?
The number of predicates is low, so I am fine using slower things like reflection of dynamic
types.
Background
The reason for the partition of predicates is that I have lots of Advanced
instances. I want to pre-filter them first by predicates that require only the IBasic
interface as those evaluate very fast. Then I will have to filter a much lower number of instances in the second pass with complex methods of Advanced
(not in IBasic
) since they take very long to evaluate.