从 MemberExpression 中取出对象? [英] Getting the object out of a MemberExpression?
本文介绍了从 MemberExpression 中取出对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以,假设我在 C# 中有以下表达式:
So, lets say I have the following expression in C#:
Expression<Func<string>> expr = () => foo.Bar;
如何提取对 foo 的引用?
How do I pull out a reference to foo?
推荐答案
Expression<Func<string>> expr = () => foo.Bar;
var me = (MemberExpression)((MemberExpression)expr.Body).Expression;
var ce = (ConstantExpression)me.Expression;
var fieldInfo = ce.Value.GetType().GetField(me.Member.Name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
var value = (Foo)fieldInfo.GetValue(ce.Value);
这篇关于从 MemberExpression 中取出对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文