Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
135 views
in Technique[技术] by (71.8m points)

c# - Getting the object out of a MemberExpression?

So, lets say I have the following expression in C#:

Expression<Func<string>> expr = () => foo.Bar;

How do I pull out a reference to foo?

question from:https://stackoverflow.com/questions/1613239/getting-the-object-out-of-a-memberexpression

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I had the same problem, but somewhat more complex, and Darin Dimitrov's answer gave me a good start. I'll post my results here, despite the fact that this is an "old" question .


Case 1: The root object is an instance member

    this.textBox.Text    // where 'this' has type 'Form'

... is equivalent to the following expression tree:

.                                    +====================+
.                                    |  MemberExpression  |
.                                    +====================+
#                                      |                |
#                          .Expression |                | .Member
#                                      v                v
.                    +------------------+              +------------+
.                    | MemberExpression |              | MemberInfo |
.                    +------------------+              +------------+
#                      |              |                 .Name = "Text"
#          .Expression |              | .Member         .MemberType = Property
#                      v              v
.   +--------------------+          +------------+
.   | ConstantExpression |          | MemberInfo |
.   +--------------------+          +------------+
#    .Value = this                   .Name = "textBox"
#    .Type  = typeof(Form)           .MemberType = Field

The only place in this expression tree where you actually get an object reference is from the ConstantExpression: it allows you to get a reference to this. The basic idea to get any object reference in this tree is thus as follows:

  1. Descend into the expression tree along the .Expression axes until you reach a ConstantExpression node.

  2. Grab that node's .Value property. This is the root object reference (ie. this in the above example).

  3. Using reflection and the MemberInfo nodes from the expression tree, get object references and work your way back "up" the expression tree.

Here's some code that demonstrates this:

Expression expr = ...;   // <-- initially set to the expression tree's root

var memberInfos = new Stack<MemberInfo>();

// "descend" toward's the root object reference:
while (expr is MemberExpression)
{
    var memberExpr = expr as MemberExpression;
    memberInfos.Push(memberExpr.Member);
    expr = memberExpr.Expression
}

// fetch the root object reference:
var constExpr = expr as ConstantExpression;
var objReference = constExpr.Value;

// "ascend" back whence we came from and resolve object references along the way:
while (memberInfos.Count > 0)  // or some other break condition
{
    var mi = memberInfos.Pop();
    if (mi.MemberType == MemberTypes.Property)
    {
        objReference = objReference.GetType()
                                   .GetProperty(mi.Name)
                                   .GetValue(objReference, null);
    }
    else if (mi.MemberType == MemberTypes.Field)
    {
        objReference = objReference.GetType()
                                   .GetField(mi.Name)
                                   .GetValue(objReference);
    }
}

Case 2: The root object is a static class member

    Form.textBox.Text    // where 'textBox' is a static member of type 'Form'

... results in a different expression tree. Note to the null reference at the lower left:

.                                    +====================+
.                                    |  MemberExpression  |
.                                    +====================+
#                                      |                |
#                          .Expression |                | .Member
#                                      v                v
.                    +------------------+              +------------+
.                    | MemberExpression |              | MemberInfo |
.                    +------------------+              +------------+
#                      |              |                 .Name = "Text"
#          .Expression |              | .Member         .MemberType = Property
#                      v              v
.                     null          +------------+
.                                   | MemberInfo |
.                                   +------------+
#                                   .Name = "textBox"
#                                   .MemberType = Field
#                                   .DeclaringType = typeof(Form)

Here, you cannot stop the "descend" phase by waiting for a ConstantExpression. Instead, you stop descending when you reach a null reference. Next, you retrieve the root object reference as follows:

var mi = memberInfos.Pop();
objReference = mi.DeclaringType
                 .GetField(member.Name, BindingFlags.Static)  // or .GetProperty!
                 .GetValue(null);

The "ascend" phase from there onwards is the same as before.


There are certainly more cases (such as named parameters as the root object), but I hope that by now, I've got the basic idea across, so I'll cut off here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...