Linq Expressions: Assign private fields (.NET 4)

Hold your horses, you might not see this untill 2010

In one of the PDC sessions I heard that the Linq.Expressions namespace have been extended so that it now contains expressions for pretty much everything that the new DLR can do.

Since my old post “Linq Expressions: Access private fields” is by far my most read blog entry, I figured that I have to throw you some more goodies.

So here it is, the code for assigning private fields using pure Linq Expressions:

public static Action<T, I> GetFieldAssigner<T, I>(string fieldName)
{

   ///change this to a recursive call that finds fields in base classes to..)
    var field = typeof(T).GetField (fieldName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); 

    ParameterExpression targetObject =
    Expression.Parameter(typeof(T), "target");

    ParameterExpression fieldValue =
    Expression.Parameter(typeof(I), "value");

    var assignment = Expression.AssignField(targetObject, field, fieldValue);

    LambdaExpression lambda =
    Expression.Lambda(typeof(Action<T, I>), assignment, targetObject, fieldValue);

    var compiled = (Action<T, I>)lambda.Compile();
    return compiled;
}

The code works pretty much the same way that my old field reader did except that this one will return an Action delegate that takes two args: target and fieldValue and returns void.

Some sample usage code:

Person roger = .....
var assigner = GetFieldAssigner<Person,string> ("firstName");
assigner(roger, "Roggan");

By using this approach you get a nice performance boost of about 300 times vs. using reflection with FieldInfo.SetValue.
(Assuming that you cache the assigner that is)

Pretty sweet.

BTW. Anyone know if this is available in the currently released DLR ?
In that case you won’t have to wait for .NET 4.

//Roger

One thought on “Linq Expressions: Assign private fields (.NET 4)”

  1. Dear Roger, the “Expression” class in .Net 4 has no “AssignField” method (3 arguments). But it has the “Assign” method (2 arguments). Please advise, where your “AssignField” method comes from?

    Thanks, Peter.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s