如何获取表达式中引用类型的值?

发布于 2024-08-26 15:56:11 字数 526 浏览 4 评论 0原文

我有这个方法:

public void DoSomething<T>(Expression<Func<T, object>> method)
{
}

如果像这样调用这个方法:

DoSomething(c => c.SomeMethod(new TestObject()));

... 如何获取传递到 SomeMethod() 中的参数值?

如果参数是值类型,则这有效:

var methodCall = (MethodCallExpression)method.Body;
var parameterValue = ((ConstantExpression)methodCall.Arguments[0]).Value;

但是,当我传入引用类型时,methodCall.Arguments[0] 是一个 MemberExpression,我似乎不知道如何编写代码来获取值它。

I have this method:

public void DoSomething<T>(Expression<Func<T, object>> method)
{
}

If this method is called like this:

DoSomething(c => c.SomeMethod(new TestObject()));

... how do I get the value of the parameter that was passed into SomeMethod()?

If the parameter is a value type, this works:

var methodCall = (MethodCallExpression)method.Body;
var parameterValue = ((ConstantExpression)methodCall.Arguments[0]).Value;

However, when I pass in a reference type, methodCall.Arguments[0] is a MemberExpression, and I can't seem to figure out how to write code to get the value out of it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

唠甜嗑 2024-09-02 15:56:11

这是答案(受到 Akash 答案的启发):

LambdaExpression lambda = Expression.Lambda(methodCall.Arguments[0]);
var compiledExpression = lambda.Compile();
return compiledExpression.DynamicInvoke();

Here is the answer (inspired by Akash's answer):

LambdaExpression lambda = Expression.Lambda(methodCall.Arguments[0]);
var compiledExpression = lambda.Compile();
return compiledExpression.DynamicInvoke();
爱殇璃 2024-09-02 15:56:11

您必须手动计算成员表达式,MemberExpression 包含“Expression”,它是指定的 Member 的容器对象,为此,您可以具体化您的论证的 Lamda 并编译和执行它。

LamdaExpression l = Expression.Lambda(methodCall.Arguments[0]);
var c = l.Compile();
var v = c.Invoke();

所以无论你传递什么,你都会在“v”变量中得到它。

You will have to evaluate member expression manually, MemberExpression contains "Expression" which is the container object of Member specified, to do this you can crete Lamda of your arguement and compile and execute it.

LamdaExpression l = Expression.Lambda(methodCall.Arguments[0]);
var c = l.Compile();
var v = c.Invoke();

So whatever you pass, you will get it in "v" variable.

寒冷纷飞旳雪 2024-09-02 15:56:11

这实际上并不是值类型或引用类型的问题,而是常量表达式或非常量表达式的问题。我确信如果您

DoSomething(c => c.SomeMethod(DateTime.Now));

也调用,您现有的代码也会失败。

基本上,该方法的参数只是一个表达式。这不是一个值。您可以编译该表达式,然后执行它以获取该时间点的值,但重要的是要了解表达式本身并不是一个值。对于常量表达式,这很简单,但以 DateTime.Now 为例,根据定义,表达式的计算值会随着时间的推移而变化:)

您想对参数做什么?这里的大局是什么?

This isn't really a matter of value type or reference type - it's a matter of a constant expression or not-constant expression. I'm sure your existing code would fail if you called

DoSomething(c => c.SomeMethod(DateTime.Now));

as well.

Basically, the argument to the method is just an expression. It's not a value. You could potentially compile that expression and then execute it to get the value at that point in time, but it's important to understand that an expression isn't a value in itself. In the case of a constant expression it's easy, but taking the DateTime.Now example, by definition the evaluated value of the expression changes over time :)

What are you trying to do with the argument? What's the bigger picture here?

关于从前 2024-09-02 15:56:11

首先,乔恩说的话。

没有可获取的值,这只是表达式。可能感兴趣的是 NewExpression,它有一个 Constructor 属性;此属性包含反射的构造函数,如果您编译表达式并运行生成的委托,则将调用该构造函数。您可以手动调用该构造函数并获取用户想要实例化的实例。

Firstly, what Jon said.

There are no values to get ahold of, it's all just expressions. What might be of interest is the NewExpression, which has a Constructor property; this property contains the reflected constructor that would be called if you compiled expression and ran the resulting delegate. You could manually invoke that constructor and get an instance of what the user was intending on instantiating.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文