我有幾個實現以下 IInactive 接口的實體框架類。public interface IInactive{ bool inactive { get; set; }}例如我的Order類定義如下:public class Order : IInactive{ [Key] [Required] public Guid orderId { get; set; } ... public bool inactive { get; set; }} 我正在嘗試實現一個通用方法,該方法可用于所有對象(實體),無論它們是否實現 IInactive 接口。它將被稱為如下:var query = GetAllActive<Order>();我的這個通用方法的代碼如下所示:public IQueryable<T> GetAllActive<T>() where T : class{ DbSet<T> dbSet = this._db.Set<T>(); // Does the entity implement the IInactive interface. // If yes, only return "active" row, otherwise return all rows if (typeof(T)is IInactive) { // Problem: the code in this block never executes, that is, // (typeof(T)is IInactive) never evaluates to true ... } return dbSet;}我非常感謝您幫助解決這個問題!謝謝。
1 回答

慕運維8079593
TA貢獻1876條經驗 獲得超5個贊
代替
if (typeof(T) is IInactive)
嘗試
if (typeof(IInactive).IsAssignableFrom(typeof(T)))
- 1 回答
- 0 關注
- 93 瀏覽
添加回答
舉報
0/150
提交
取消