我有一个抽象数据实体MyDataRow
,它继承了DataRowA
。
有一个泛型类使用它BusinessRow<TData> where TData:MyDataRow
。问题是我想要根据字符串变量创建BusinessRow的新对象:
public BusinessRow<MyDataRow> AddNewRow(string rowType)
并在其泛型类型中使用MyDataRow的继承类之一:
public class BusinessRow<TData>
where TData : MyDataRow
{
}
public abstract class MyDataRow
{
}
public class DataRowA : MyDataRow
{
}
public class Main
{
public BusinessRow<MyDataRow> AddNewRow(string rowType)
{
BusinessRow<MyDataRow> row = null;
switch (rowType)
{
case "A":
default:
row =new BusinessRow<DataRowA>();
// Error CS0029 Cannot implicitly convert type
// 'BusinessRow<DataRowA>' to 'BusinessRow<MyDataRow>'
break;
}
return row;
}
}
在以下代码中
BusinessRow<MyDataRow> row = new BusinessRow<DataRowA>();
我收到此错误
Error CS0029 Cannot implicitly convert type
'BusinessRow<DataRowA>' to 'BusinessRow<MyDataRow>'