c#PropertyInfo提取内部表达式

c#PropertyInfo提取内部表达式,c#,reflection,expression,C#,Reflection,Expression,我必须使用反射来循环反射对象的某些属性,并收集它们的PropertyInfo对象 其中一些属性的类型为Expression,我必须从属性信息中提取底层表达式 我尝试将myPropertyInfo.GetValue(parParameter)作为LambdaExpression,但它似乎不起作用 谁能给我一些指点吗 将myPropertyInfo.GetValue(parParameter)用作LambdaExpression是可疑的,因为参数和表达式是两个不同的东西。在你进行反射之后,你似乎把你

我必须使用反射来循环反射对象的某些属性,并收集它们的
PropertyInfo
对象

其中一些属性的类型为
Expression
,我必须从属性信息中提取底层表达式

我尝试将myPropertyInfo.GetValue(parParameter)作为LambdaExpression,但它似乎不起作用


谁能给我一些指点吗

myPropertyInfo.GetValue(parParameter)用作LambdaExpression是可疑的,因为参数和表达式是两个不同的东西。在你进行反射之后,你似乎把你的变量弄混了。下面是一个可能有助于澄清问题的示例:

class Type1 { public string Name { get; set; } }
class Data { public Expression<Func<Type1, string>> Ex { get; set; } }
class Program
{
    static void Main(string[] args)
    {
        var d = new Data { Ex = t => t.Name };
        var pi = d.GetType().GetProperties().Single();
        var ex = pi.GetValue(d) as LambdaExpression;
        Console.WriteLine(pi.GetValue(d).GetType());
        Console.WriteLine(ex);
        Console.WriteLine(ex.Parameters.Single());
    }
}
class Type1{公共字符串名称{get;set;}
类数据{public Expression Ex{get;set;}}
班级计划
{
静态void Main(字符串[]参数)
{
var d=新数据{Ex=t=>t.Name};
var pi=d.GetType().GetProperties().Single();
var ex=pi.GetValue(d)作为LambdaExpression;
WriteLine(pi.GetValue(d.GetType());
控制台写入线(ex);
Console.WriteLine(例如Parameters.Single());
}
}

myPropertyInfo.GetValue(parParameter)用作LambdaExpression是可疑的,因为参数和表达式是两个不同的东西。在你进行反射之后,你似乎把你的变量弄混了。下面是一个可能有助于澄清问题的示例:

class Type1 { public string Name { get; set; } }
class Data { public Expression<Func<Type1, string>> Ex { get; set; } }
class Program
{
    static void Main(string[] args)
    {
        var d = new Data { Ex = t => t.Name };
        var pi = d.GetType().GetProperties().Single();
        var ex = pi.GetValue(d) as LambdaExpression;
        Console.WriteLine(pi.GetValue(d).GetType());
        Console.WriteLine(ex);
        Console.WriteLine(ex.Parameters.Single());
    }
}
class Type1{公共字符串名称{get;set;}
类数据{public Expression Ex{get;set;}}
班级计划
{
静态void Main(字符串[]参数)
{
var d=新数据{Ex=t=>t.Name};
var pi=d.GetType().GetProperties().Single();
var ex=pi.GetValue(d)作为LambdaExpression;
WriteLine(pi.GetValue(d.GetType());
控制台写入线(ex);
Console.WriteLine(例如Parameters.Single());
}
}

您可以在这里很好地使用
动态
吗?请显示您目前拥有的代码,以便更容易地查看问题发生的位置。您知道编译时的类型
Type1
string
?我不确定,但我认为就类型而言,
表达式
不是
lambdaexpression
。您想用该值做什么?可能是您可以在这里很好地使用
dynamic
?请显示您目前拥有的代码,以便更容易地查看问题发生的位置。您知道编译时的类型
Type1
string
?我不确定,但我认为就类型而言,
表达式
不是
lambdaexpression
。你想用这个值做什么?