EntityBase是基类IBLLBase<T> where T:EntityBase,new() 是基类接口Test是实体类TestBLL是接口实现类下面是某个类里面的的索引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>不支持协变,而且你的T限制了只能是EntityBase,因此答案就是“不能”。话说我很好奇你这么做意义很在,感觉除了测试,没什么用。
 
                    
                    
                            慕码人2483693
                            
                                
                            
                        
                        
                                                
                    TA贡献1860条经验 获得超9个赞
class Base { 
    
    }    //协变定义,流畅转换成子类型
    interface IBase<out T> where T : Base, new() {        void SomeMethod();        
    }    class Derived:Base { 
    
    }    class Implementation : IBase<Derived> {        public void SomeMethod()
        {
        }
    }    class DoSomething {        //dynamic 不能作为类型参数
        public IBase<Base> this[string name]
        {            get {                if (name == "")
                {                    return new Implementation();
                }                else
                    return null;
            }
        }
    }- 2 回答
- 0 关注
- 1381 浏览
添加回答
举报
0/150
	提交
		取消
	