2017-08-27 65 views
1

我有兩個接口通用區別

public interface IEntity<TKey> { 
} 

public interface IEntity : IEntity<int> { 
} 

我知道我可以檢查一個類型爲IEntityif (entity is IEntity)但我怎麼能檢查它是否是更普遍的IEntity<TKey>對象?

另外,我怎樣才能安全地將通用實體轉換爲接口類型?

+0

您需要說明最後一部分關於演員表 – Nkosi

+0

Nevermind。你的回答給了我這個主意! :) 謝謝! – Lorenzo

回答

2

請看以下使用反射的示例。

Type targetType = typeof(IEntity<>); 
var entityType = entity.GetType(); 
if (entityType.IsGenericType 
    && targetType.IsAssignableFrom(entityType.GetGenericTypeDefinition())) { 

}