C# 如何从IQueryable<;T>;?
我有一个返回T的IQueryable的方法。使用反射,我得到的是方法的返回类型,它是T的IQueryable。我想使用反射来提供T的所有属性,但当我使用以下代码时,我没有得到属性:C# 如何从IQueryable<;T>;?,c#,reflection,C#,Reflection,我有一个返回T的IQueryable的方法。使用反射,我得到的是方法的返回类型,它是T的IQueryable。我想使用反射来提供T的所有属性,但当我使用以下代码时,我没有得到属性: //Get return type of method and then get its Public properties var returnType = methodInfo.ReturnType; // returns typeof(IQueryable<T>)
//Get return type of method and then get its Public properties
var returnType = methodInfo.ReturnType; // returns typeof(IQueryable<T>)
var returnTypePropertyInfos = returnType.GetProperties();
var propertySpecs = returnTypePropertyInfos.Select(returnTypePropertyInfo => new VPropertySpec()
{
PropertyName = returnTypePropertyInfo.Name, PropertyType = returnTypePropertyInfo.PropertyType.Name
}).ToList();
//获取方法的返回类型,然后获取其公共属性
var returnType=methodInfo.returnType;//返回typeof(IQueryable)
var returnTypePropertyInfos=returnType.GetProperties();
var propertySpecs=returnTypePropertyInfos.Select(returnTypePropertyInfo=>new VPropertySpec()
{
PropertyName=returnTypePropertyInfo.Name,PropertyType=returnTypePropertyInfo.PropertyType.Name
}).ToList();
假设属性类型为IQueryable
您可以执行以下操作:
returnTypePropertyInfo.PropertyType.GetGenericArguments()[0].GetProperties();
我将如何与LINQ合作:
var propertySpect =
from argument in methodInfo.ReturnType.GetGenericArguments()
from property in argument.GetProperties()
select new VPropertySpec() {
PropertyName = property.Name, PropertyType = property.PropertyType.Name,
// Maybe add for multiple generic arguments: GenericArgument = argument,
};
试试这个。。typeof(T).GetProperties();您是如何创建MethodInfo的?读了这篇文章,我发现有一个例子说明了你在寻找什么(也许)对不起,你是说下一句话吗?var returnTypePropertyInfos=returnType.GetGenericArguments()[0].GetProperties();否则我就不明白了。解决办法就是ntl说的。var returnTypePropertyInfos=returnType.GetGenericArguments()[0].GetProperties();谢谢你的帮助。