![](/img/trans.png)
[英]Unable to cast object of type 'System.Collections.Generic.List`1[System.Object]' to type 'System.Collections.Generic.List`1[customType]
[英]Unable to cast object of type System.Collections.Generic.List[System.Object] to type System.Collections.Generic.IList[ShortCodeList]
BindingProperties 返回类型 T。 linq 查询在运行时带来的结果是System.Collections.Generic.IList[ShortCodeList]
但是当我尝试使用 IList T> 对其进行类型转换时,它返回:
无法将“System.Collections.Generic.List`1[System.Object]”类型的对象转换为“System.Collections.Generic.IList`1[ModelAccessLayer.Model.ShortCodeList]”
private IList<T> GetResultSetConvertedToList<T>(IList<T> dataSet, IEnumerable<dynamic> resultSet) where T : class, new()
{
IList<T> customResult = (IList<T>)(resultSet.Select(x => BindingProperties(new T(), x)).ToList());
return customResult;
}
有人可以帮我解决这个问题吗?
select 表达式中的类型dynamic
导致整个表达式被视为动态表达式,返回一个IEnumerable<dynamic>
如果您明确告诉 C# 使用.Select<dynamic,T>
,您可以强制返回类型为 T 并且您的代码将按预期工作。
private IList<T> GetResultSetConvertedToList<T>(IList<T> dataSet, IEnumerable<dynamic> resultSet)
where T : class, new()
{
IList<T> customResult = (IList<T>)(resultSet.Select<dynamic, T>(x => BindingProperties(new T(), x)).ToList());
return customResult;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.