Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an Expression<Func<T, object>> variable by reflection

I have created the following class:

public class Person
{
     public string FirstName { get; set; }
     public string LastName { get; set; }
}

I am able to set the following statement to a method parameter:

myClass.SetFieldName<Person>(p => p.LastName);

The type of the parameter is:

Expression<Func<Person, object>>

Now what I am trying to accomplish is to call the SetFieldName Method for a property found by reflection. Imagine I have an instance of PropertyInfo (for Person.LastName). I tried to create the Expression by using its Lambda method, but I failed.

So it would be very nice, if you can help me with this.

Regards, Koray

like image 366
Koray Kazgan Avatar asked Aug 25 '12 09:08

Koray Kazgan


2 Answers

You just need to expand a little bit the flem's answer:

using System;
using System.Linq.Expressions;
using NUnit.Framework;

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public static class ExpressionBuilder
{
    public static Expression<Func<TClass, TProperty>> Build<TClass, TProperty>(string fieldName)
    {
        var param = Expression.Parameter(typeof(TClass));
        var field = Expression.PropertyOrField(param, fieldName);
        return Expression.Lambda<Func<TClass, TProperty>>(field, param);
    }
}

[TestFixture]
public class Test
{
    [Test]
    public void TestExpressionBuilder()
    {
        var person = new Person { FirstName = "firstName", LastName = "lastName" };
        var expression = ExpressionBuilder.Build<Person, string>("FirstName");

        var firstName = expression.Compile()(person);

        Assert.That(firstName, Is.EqualTo(person.FirstName));
    }
}
like image 79
Artiom Ciumac Avatar answered Sep 30 '22 11:09

Artiom Ciumac


// reflected field name
string fieldName = "LastName";

// create the parameter of the expression (Person)
ParameterExpression param = Expression.Parameter(typeof(Person), string.Empty);

// create the expression as a get accessor of a particular 
// field of the parameter (p.LastName)
Expression field = Expression.PropertyOrField(param, fieldName);
like image 41
Paul Fleming Avatar answered Sep 30 '22 13:09

Paul Fleming