[英]How to get the entities from generic EntitySet on run-time without knowing the generic type?
我正在创建一个映射器函数,该函数将在运行时使用具有未知不同通用类型的EntitySet<>
,我想在传入的EntitySet
获取实体并将其添加到列表中。
我无法访问实体,因为我将EntitySet
作为对象来了,并且当我尝试将对象转换为各自的类型时,我失败了,因为我不知道EntitySet
的通用类型
((EntitySet<XXXX>)dataValues[pInfo.Name]).ToList();
我可以读取通用EntitySet
的数据类型,但无法使用它进行演员表转换,我不知道如何或什至不可以!
dataValues[pInfo.Name].GetType().GenericTypeArguments.First()
我不太在乎泛型类型,因为我只需要将集合放入EntitySet
。
我知道我的问题看起来很重复,但是老实说,我搜索时发现的结果无法解决我的问题。
我正在使用C#
和ASP.net
EntySet继承自System.Collections.Generic.ICollection,System ICollection继承自System.Collections.Generic.IEnumerable,而IEnumerable继承自System.Collections.IEnumerable。
System.Collections.IEnumerable不是通用接口。 您可以在ineach中使用。
所以你的功能是这样的:
public class Example
{
public static void EnumEntities( IEnumerable entities )
{
foreach( var entity in entities )
{
Console.WriteLn( entity.ToString());
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.