Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从IQueryable<;T>;?_C#_Reflection - Fatal编程技术网

C# 如何从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>)

我有一个返回T的IQueryable的方法。使用反射,我得到的是方法的返回类型,它是T的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();谢谢你的帮助。