Provo a giocare un po 'con gli alberi di espressione. Ho un oggetto con un List<string>
e voglio costruire un albero di espressioni che aggiunga un valore a questa proprietà, ma voglio specificare il valore da aggiungere attraverso un Func
. Attualmente provo questo ...
public static Action<T> CreateMethodAddObjectToList<T, C>(this Type type, string property, Func<C> ctorFunction)
{
PropertyInfo fieldInfo = type.GetProperty(property);
if (fieldInfo == null)
{
return null;
}
ParameterExpression targetExp = Expression.Parameter(type, "target");
MemberExpression fieldExp = Expression.Property(targetExp, property);
var method = fieldExp.Type.GetMethod("Add", BindingFlags.Public | BindingFlags.Instance);
Expression<Func<C>> ctorExpression = () => ctorFunction();
// but this doesnt work because I can't use the ctorExpression in this way
var callExp = Expression.Call(fieldExp, method, ctorExpression);
var function = Expression.Lambda<Action<T>>(callExp, targetExp).Compile();
return function;
}
la chiamata sembra
var dummyObject = new DummyObject { IntProperty = 5 };
Action<DummyObject> setter = typeof (DummyObject).CreateMethodAddObjectToList<DummyObject, string>("StringList", () => "Test" );
È possibile modificare ctorFunction in Expression<Func<C>>
e quindi richiamarla nell'azione generata:
public static Action<T> CreateMethodAddObjectToList<T, C>(this Type type, string property, Expression<Func<C>> createExpr)
{
PropertyInfo fieldInfo = type.GetProperty(property);
if (fieldInfo == null)
{
return null;
}
ParameterExpression targetExp = Expression.Parameter(type, "target");
MemberExpression fieldExp = Expression.Property(targetExp, property);
var method = fieldExp.Type.GetMethod("Add", BindingFlags.Public | BindingFlags.Instance);
var valueExpr = Expression.Invoke(createExpr);
var callExpr = Expression.Call(fieldExp, method, valueExpr);
var function = Expression.Lambda<Action<T>>(callExpr, targetExp).Compile();
return function;
}