I am trying to check if a type implements the generic ICollection<T> interface, since this is a base interface for any of my generic collections.
The below code doesn't work
GetType(ICollection(Of)).IsAssignableFrom(
objValue.GetType().GetGenericTypeDefinition())
What's a good way of detecting if a type implements a generic interface?
CustomCollection c = new CustomCollection();
bool implementICollection = c.GetType().GetInterfaces()
.Any(x => x.IsGenericType &&
x.GetGenericTypeDefinition() == typeof(ICollection<>));
An alternative to the others is the following:
if (MyObject is ICollection<T>)
...
Note: This will only work if T
is known at compile time.
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