Koray Kazgan
Koray Kazgan

Reputation: 101

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

Upvotes: 7

Views: 5981

Answers (3)

Rookian
Rookian

Reputation: 20539

public static class LambdaExpressionExtensions
{
    public static Expression<Func<TInput, object>> ToUntypedPropertyExpression<TInput, TOutput> (this Expression<Func<TInput, TOutput>> expression)
    {
        var memberName = ((MemberExpression)expression.Body).Member.Name;

        var param = Expression.Parameter(typeof(TInput));
        var field = Expression.Property(param, memberName);
        return Expression.Lambda<Func<TInput, object>>(field, param);
    }
}

Upvotes: 0

Artiom Ciumac
Artiom Ciumac

Reputation: 414

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));
    }
}

Upvotes: 3

Paul Fleming
Paul Fleming

Reputation: 24526

// 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);

Upvotes: 2

Related Questions