我有两个接口
public interface IEntity<TKey> {
}和
public interface IEntity : IEntity<int> {
}我知道我可以通过执行if ( entity is IEntity )来检查类型是否为IEntity,但是如何检查它是否更一般地是一个IEntity<TKey>对象?
另外,如何将泛型实体安全地转换为接口类型?
发布于 2017-08-27 22:54:56
请看下面使用反射的示例。
Type targetType = typeof(IEntity<>);
var entityType = entity.GetType();
if (entityType.IsGenericType
&& targetType.IsAssignableFrom(entityType.GetGenericTypeDefinition())) {
}https://stackoverflow.com/questions/45906198
复制相似问题