I've got an IList<Delegate>
that contains some Func<bool>
s and some Predicate<T>
s, where T
varies. I later need to sort out which of these items are Predicate<T>
s, but don't want to close the door to adding other Delegate
types to the list later, so I do not want to do this by identifying objects by !(current_delegate is Func<bool>)
.
The highest abstraction below Predicate<T>
is MulticastDelegate
, which seems unhelpful (would need a non-generic Predicate
type under Predicate<T>
), and identifying the presence of the generic parameter is also useless given the other generic Delegate
s that may be present in the list.
The only other thing I've considered is checking the Name
of the Type
. To me, string comparison is a near-smell, but maybe that is the is the best and/or only way -- you tell me.
What is the best way to definitively determine that an object
is any Predicate<T>
without knowing the type of T
?
Like this:
obj.GetType().GetGenericTypeDefinition() == typeof(Predicate<>)
Predicate<int> pred = ...;
var isPreidcate = pred.GetType().GetGenericTypeDefinition() == typeof(Predicate<>);
On another note, if you have a generic list, you shoulnd't need to check the types in it. You may want to rethink your design if you need to check for specific types within a list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With