Reputation: 29427
I have two interfaces
public interface IEntity<TKey> {
}
and
public interface IEntity : IEntity<int> {
}
I know I can check if a type is IEntity
by doing if ( entity is IEntity )
but how can I check if it's more generally an IEntity<TKey>
object?
Also, how can i cast safely the generic entity to the interface type?
Upvotes: 1
Views: 57
Reputation: 247018
Look at the following example using reflection.
Type targetType = typeof(IEntity<>);
var entityType = entity.GetType();
if (entityType.IsGenericType
&& targetType.IsAssignableFrom(entityType.GetGenericTypeDefinition())) {
}
Upvotes: 2