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 關注
- 400 瀏覽
添加回答
舉報