tags:

views:

203

answers:

1

For the following Lambda expression:

GetPropertyNameAndArrayIndex(() => SomeArray[0])

I know that you can get the property name for an expression. I also know that you can get the array index by using a ConstantExpression and accessing the Right value. My question is how do you get array index (or Right value) when it is not a constant, I.E.,

for (int i = 0; i < 5; i++)
{
    GetPropertyNameAndArrayIndex(() => SomeArray[i])
}

Any help would be greatly appreciated.

+2  A: 

As already noted in comments; note that the example given is susceptible to issues with captured variables if used asynchronously - but probably OK "as is".

To do it thoroughly involves a lot of edge-cases (or you can cheat and use Compile()) - but here's an example that shows the overall themes (without using Compile):

using System;
using System.Linq.Expressions;
using System.Reflection;
class Program
{
    static void Main()
    {
        string[] arr = { "abc", "def" };
        for (int i = 0; i < arr.Length; i++)
        {
            Foo(() => arr[i]);
        }
    }
    static object Foo<T>(Expression<Func<T>> lambda)
    {
        object obj = Walk(lambda.Body);
        Console.WriteLine("Value is: " + obj);
        return obj;

    }
    static object Walk(Expression expr)
    {
        switch (expr.NodeType)
        {
            case ExpressionType.ArrayIndex:

                BinaryExpression be = (BinaryExpression)expr;
                Array arr = (Array)Walk(be.Left);
                int index = (int) Walk(be.Right);
                Console.WriteLine("Index is: " + index);
                return arr.GetValue(index);
            case ExpressionType.MemberAccess:
                MemberExpression me = (MemberExpression)expr;
                switch (me.Member.MemberType)
                {
                    case MemberTypes.Property:
                        return ((PropertyInfo)me.Member).GetValue(Walk(me.Expression), null);
                    case MemberTypes.Field:
                        return ((FieldInfo)me.Member).GetValue(Walk(me.Expression));
                    default:
                        throw new NotSupportedException();
                }
            case ExpressionType.Constant:
                return ((ConstantExpression) expr).Value;
            default:
                throw new NotSupportedException();

        }
    }

}
Marc Gravell
@Marc I am humbled... thanks very much
Kane
@Marc - Is this an exhaustive list of walkables?
Maslow
@Maslow absolutely not. I have a fuller 3.5 list somewhere, but this grows again in 4.0. The trick is to figure out what range of scenarios you need to support.
Marc Gravell
@Marc - well I'm hoping to approach exhaustive for my project http://maybe.codeplex.com/
Maslow
@Maslow - didn't we discuss that one by e-mail at some point? Sounds familiar.
Marc Gravell