EntityBase是基類IBLLBase<T> where T:EntityBase,new() 是基類接口Test是實(shí)體類TestBLL是接口實(shí)現(xiàn)類下面是某個(gè)類里面的的索引public IBLLBase<dynamic> this[string name]{ get { switch (name.ToLower()) { case"test": return (IBLLBase<Test>)new TestBLL(); } return null; }}錯(cuò)誤:Test無(wú)法轉(zhuǎn)換成dynamic類型。誰(shuí)知道怎么做才能把Test轉(zhuǎn)換成dynamic?
2 回答

慕勒3428872
TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超6個(gè)贊
因?yàn)槟愕腎BLLBase<T>不支持協(xié)變,而且你的T限制了只能是EntityBase,因此答案就是“不能”。話說(shuō)我很好奇你這么做意義很在,感覺除了測(cè)試,沒什么用。

慕碼人2483693
TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超9個(gè)贊
class Base { } //協(xié)變定義,流暢轉(zhuǎn)換成子類型 interface IBase<out T> where T : Base, new() { void SomeMethod(); } class Derived:Base { } class Implementation : IBase<Derived> { public void SomeMethod() { } } class DoSomething { //dynamic 不能作為類型參數(shù) public IBase<Base> this[string name] { get { if (name == "") { return new Implementation(); } else return null; } } }
- 2 回答
- 0 關(guān)注
- 1371 瀏覽
添加回答
舉報(bào)
0/150
提交
取消