When calling any Convert function error apperars:
Argument 2: cannot convert from 'System.Collections.Generic.List<T>' to 'System.Collections.Generic.List<ProductionRecent>
Im trying to pass any list inside the function, determine which list it must be and converting it. Any Suggestions?
public List<T> ConvertToList<T>(DataTable dt, List<T> list)
{
if (list.GetType() == typeof(List<ProductionPending>))
{
ConvertToProductionPending(dt, list); // ERROR
}
else if (list.GetType() == typeof(List<ProductionRecent>))
{
ConvertToProductionRecent(dt, list); // ERROR
}
else if (list.GetType() == typeof(List<MirrorDeployments>))
{
ConvertToMirror(dt list); // ERROR
}
return list;
}
private List<ProductionPending> ConvertToProductionPending(DataTable dt, List<ProductionPending> list)
{
// do some stuff here
return list;
}
private List<ProductionRecent> ConvertToProductionRecent(DataTable dt, List<ProductionRecent> list)
{
// do some stuff here
return list;
}
private List<MirrorDeployments> ConvertToMirror(DataTable dt, List<MirrorDeployments> list)
{
// do some stuff here
return list;
}
Try to cast before pass to your method:
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;
}
Edit:
Also, if you're just returning the list without doing anything, you don't need the convert method at all, just cast like List<MirrorDeployments> l2 = (list as List<MirrorDeployments>)
If you're using C# 7, you could also use pattern matching :
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.