[英]Runtime-Type of T in IQueryable<T>
Is there a possibility to get the T-Runtime type of an IQueryable?是否有可能获得 IQueryable 的 T-Runtime 类型? If it's a normal list, there is no problem, but if I check against an IQueryable, I always get a type of System.Object.
如果它是一个普通列表,则没有问题,但是如果我对照 IQueryable 进行检查,我总是会得到 System.Object 的类型。 My code is looking like this at the moment:
我的代码目前看起来像这样:
private static Type GetDtoTypeFromDataSource(ReportDataSource dataSourceFromDb)
{
Type result = null;
if (dataSourceFromDb.Data.GetType().IsGenericType)
{
Type[] genericArguments = dataSourceFromDb.Data.GetType().GetGenericArguments();
result = genericArguments.First();
if (result == typeof(Object))
{
var obj = dataSourceFromDb.Data.Cast<object>().ToList();
var t2 = obj.First().GetType();
var t = obj.GetType().GetGenericArguments();
var t3 = obj.GetType();
result = obj.GetType();
}
}
else
{
result = dataSourceFromDb.Data.GetType().GetElementType();
}
return result;
}
I hoped, if I actually load the List via "ToList" I get the Data, but doesnt work as well.我希望,如果我真的通过“ToList”加载列表,我会得到数据,但效果不佳。
var t2 = obj.First().GetType();
Would to the trick, but what if the List is empty?会的伎俩,但如果 List 是空的怎么办? This doesn't seem like a good solution to me?
这对我来说似乎不是一个好的解决方案?
The compile-time type of Data
will always be the same. 的编译时类型
Data
将始终是相同的。 From what you're describing, it sounds like it is IQueryable<object>
. 根据您的描述,听起来像是
IQueryable<object>
。 This is why you always get System.Object
when you try to examine the type. 这就是为什么在尝试检查类型时总是获得
System.Object
原因。
As to the run-time types of actual objects stored in this collection, you have to examine the objects in a way similar to what you've shown in your question: 至于存储在此集合中的实际对象的运行时类型,您必须以与问题中所示类似的方式检查对象:
dataSource.Data.First().GetType();
Considering query is IQueryable<T>
考虑查询是
IQueryable<T>
var typeOfT = query.GetType().GenericTypeArguments[0];
Works for me为我工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.