1 回答

TA貢獻1934條經驗 獲得超2個贊
假設您有這些定義:
interface IEntityBase { }
interface ITESTEntity : IEntityBase { }
class TESTEntity : ITESTEntity { }
那么你的問題就出自這里:
class myQuery<T, I>
where T : class, I
where I : IEntityBase
您聲明T必須可分配給I,并且I必須可分配給IEntityBase。沒關系,但是這里:
static void GetQuery<I>() where I : IEntityBase
{
var qry = new myQuery<TESTEntity, I>();
您接受任何I可分配給 的內容IEntityBase。所以你也可以這樣稱呼它:
interface ITESTEntity2 : IEntityBase { }
class TESTEntity2 : ITESTEntity2 { }
GetQuery<ITESTEntity2>();
此調用是有效的,但現在在您的方法中GetQuery():
new myQuery<TESTEntity, I>();
I將是ITESTEntity2,TESTEntity不可分配給它。不能保證 可以TESTEntity分配給任何可分配給 的I位置,如上面使用 演示的那樣。IIEntityBaseITESTEntity2
所以重新考慮你的設計。
- 1 回答
- 0 關注
- 158 瀏覽
添加回答
舉報