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

慕勒3428872
TA貢獻1848條經驗 獲得超6個贊
因為你的IBLLBase<T>不支持協(xié)變,而且你的T限制了只能是EntityBase,因此答案就是“不能”。話說我很好奇你這么做意義很在,感覺除了測試,沒什么用。

慕碼人2483693
TA貢獻1860條經驗 獲得超9個贊
class Base { } //協(xié)變定義,流暢轉換成子類型 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 關注
- 1346 瀏覽
添加回答
舉報
0/150
提交
取消