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
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));
}
}
// 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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With