I am tying to build an Expression tree that would represent this lamba:
Action<TFrom, TTo> map =
(from, to) =>
{
to.Property1 = (Nullable<TTo>)from.Property1;
to.Property2 = (Nullable<TTo>)from.Property2;
// ...continued for all properties
};
Essentially, I am trying to map the non-nullable properties from one class to the Nullable properties of another class that share the same property name.
I have written this (incorrect) tree in my attempt to do this:
object i = new SomeObjWithOutNullable();
object j = new SomeObjWithNullable();
ParameterExpression p1 = Expression.Parameter(typeof(SomeObjWithOutNullable), "from");
ParameterExpression p2 = Expression.Parameter(typeof(SomeObjWithNullable), "to");
MemberExpression m1 = Expression.PropertyOrField(p1, "Property1");
MemberExpression m2 = Expression.PropertyOrField(p2, "Property1");
BinaryExpression body = Expression.Assign(m1, m2);
LambdaExpression lambda = Expression.Lambda<Action<SomeObjWithOutNullable,SomeObjWithNullable>>(body, new[] { p1,p2 });
var action = lambda.Compile();
action(i,j);
This does not compile. I get this exception when I attempt to: Edit:
Delegate 'System.Action<UserQuery.SomeObjWithOutNullable,UserQuery.SomeObjWithNullable>' has some invalid arguments
Argument 1: cannot convert from 'object' to 'UserQuery.SomeObjWithNullable'
Argument 2: cannot convert from 'object' to 'UserQuery.SomeObjWithNullable'
I know I have yet to add in the type conversion, but I cannot seem to figure out how to do even the assignment correctly. Any help would be appreciated.
Edit: Since I don't know what I am doing here, I have updated to a (i think) closer representation of the lambda I am trying to build
Aucun commentaire:
Enregistrer un commentaire