1 回答
TA贡献2037条经验 获得超6个赞
尝试在传递给您的方法之前进行转换:
public List<T> ConvertToList<T>(DataTable dt, List<T> list)
{
if (list.GetType() == typeof(List<ProductionPending>))
{
ConvertToProductionPending(dt, (list as List<ProductionPending>));
}
else if (list.GetType() == typeof(List<ProductionRecent>))
{
ConvertToProductionRecent(dt, (list as List<ProductionRecent>));
}
else if (list.GetType() == typeof(List<MirrorDeployments>))
{
ConvertToMirror(dt, (list as List<MirrorDeployments>));
}
return list;
}
编辑:
另外,如果您只是返回列表而不做任何事情,则根本不需要 convert 方法,只需像 List<MirrorDeployments> l2 = (list as List<MirrorDeployments>)
如果您使用的是 C# 7,您还可以使用模式匹配:
public List<T> ConvertToList<T>(DataTable dt, List<T> list)
{
switch(list)
{
case List<ProductionPending> pp:
//pp is list cast as List<ProductionPending>
break;
case List<ProductionRecent> pr:
//pr is list cast as List<ProductionRecent>
break;
case List<MirrorDeployments> md:
//md is list cast as List<MirrorDeployments>
break;
}
return list;
}
- 1 回答
- 0 关注
- 353 浏览
添加回答
举报