I am trying to create an expression tree binding that will create an object if the property is available and place null if it is not. For example, I want to end up with:
personModel = car.Person == null ? null : new PersonModel() But no matter what I try it fails.
I tried coalesce
Expression.Coalesce( Expression.Property(param, "Person"), Expression.MemberInit(Expression.New(typeof(PersonModel)), MemberAssignment[]) ) This throws System.ArgumentException: 'Argument types do not match'. I assume the expression tree is expecting to place the same types in the coalesce - since I have a few nullable enumerates that work in the above scenario.
I tried conditional
Expression.Condition( Expression.Equal(Expression.Property(param, "Person"), Expression.Constant(null)), Expression.MemberInit(Expression.New(typeof(PersonModel)), MemberAssignment[]), Expression.Constant(null) ) This also throws System.ArgumentException: 'Argument types do not match' due to the true block having Expresison.MemberInit and the false block having Expression.Constant.
Is there anyway to make something like this work?