Setting Private Properties?

20 views
Skip to first unread message

Tim Scott

unread,
Feb 25, 2010, 1:01:48 PM2/25/10
to NBuilder
Hey, how about this extension?

public static ISingleObjectBuilder<T> WithPrivate<T, TProperty>(this
ISingleObjectBuilder<T> b, Expression<Func<T, TProperty>> property,
TProperty value)
{
b.Do(x => ((PropertyInfo)
((MemberExpression)property.Body).Member).SetValue(x, value, null));
return b;
}

More and more I'm striving to encapsulate my domain entities.
Generally, this means I setup state via methods, but sometimes it's
easier to just set a private property. This extension solves that
problem for me.

What do you think?

Tim Scott

Reply all
Reply to author
Forward
0 new messages