Evgeni
Evgeni

Reputation: 3343

Setting property without knowing target type at compile time

I want to set the property value on an object without knowing the object type at compile time; I want it to be fast (i.e. not using Reflection every time); I know property name and type.

The fastest way (afaik) is to use delegates; so this is what I have so far:

class User // this is an example.. Assume I don't know which type this is.
 {
    public string Name {get;set;}   
 }

public static Action<object, object> CreatePropertySetter(Type targetType, string propertyName)
{
    ParameterExpression targetObjParamExpr = Expression.Parameter(targetType);
    ParameterExpression valueParamExpr = Expression.Parameter(targetType.GetProperty(propertyName).PropertyType);

    MemberExpression propertyExpr = Expression.Property(targetObjParamExpr, propertyName);

    BinaryExpression assignExpr = Expression.Assign(targetObjParamExpr, valueParamExpr);

    Action<object, object> result = Expression.Lambda<Action<object, object>>(assignExpr, targetObjParamExpr, valueParamExpr).Compile();
    return result;
}

Then I'd make this call:

User user = new User();
var userNameSetter = CreatePropertySetter(user.GetType(), "Name");
userNameSetter(user, "Bob");

However, it doesn't like the fact that I pass User type object instead of Object, and fails with "ParameterExpression of type 'User' cannot be used for delegate parameter of type 'System.Object".

I'm new to expression trees, so a bit lost here. Why can't it cast User to object ? Do I need a cast somewhere ?

The "Action" doesn't look great either; would be nicer to just return a delegate taking arguments (User user, string propertyValue). Again, not sure how to accomplish that. Actually, I've tried it with Delegate.CreateDelegate, but it invokes using .Invoke() method, which is slow (is this the only way?); same with Expression.Lambda (non-generic).

Any thoughts ?

Also, is there a good (better than msdn) documentation on Expression Trees? The msdn version really lacks details.

Upvotes: 3

Views: 2824

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062915

If you want to use Expression, then: Convert...

static void Main()
{
    var setter = CreatePropertySetter(typeof (User), "Name");
    var obj = new User();
    setter(obj, "Fred");
}
public static Action<object, object> CreatePropertySetter(Type targetType, string propertyName)
{
    var target = Expression.Parameter(typeof (object), "obj");
    var value = Expression.Parameter(typeof (object), "value");
    var property = targetType.GetProperty(propertyName);
    var body = Expression.Assign(
        Expression.Property(Expression.Convert(target, property.DeclaringType), property),
        Expression.Convert(value, property.PropertyType));

    var lambda = Expression.Lambda<Action<object, object>>(body, target, value);
    return lambda.Compile();
}

However! You might want to look at FastMember (also available on NuGet), which wraps all of this up for you very conveniently (and uses raw IL for silly craziness).

If you want to use a typed delegate, you'll need to know the types in advance. If you know the types, you could add some generic:

static void Main()
{
    var setter = CreatePropertySetter<User,string>("Name");
    var obj = new User();
    setter(obj, "Fred");
}
public static Action<TType, TValue> CreatePropertySetter<TType, TValue>(string propertyName)
{
    var target = Expression.Parameter(typeof (TType), "obj");
    var value = Expression.Parameter(typeof (TValue), "value");
    var property = typeof(TType).GetProperty(propertyName);
    var body = Expression.Assign(
        Expression.Property(target, property),
        value);

    var lambda = Expression.Lambda<Action<TType, TValue>>(body, target, value);
    return lambda.Compile();
}

Upvotes: 9

Related Questions