I want to convert this:
Func<dynamic, object> myFunc = t => return t.Name + " " + t.Surname;
In the tree of expressions.
What I came up with is this:
ParameterExpression target = ExpressionParameter(typeof(dynamic), "target"); ParameterExpression result = ExpressionParameter(typeof(object), "result"); BlockExpression block = Expression.Block( new [] { result }, Expression.Assign( result, Expression.Add( Expression.Add( Expression.Property(target, "Name"), Expression.Constant(" ", typeof(string)) ), Expression.Property(target, "Surname") ) ) ); Func<dynamic, object> myFunc = Expression.Lambda<dynamic, object>>(block, target).Compile();
However, the compiler does not like typeof(dynamic) , and I kind of get it. dynamic not a type, essentially << 24>.
So, I started changing ParameterExpression :
ParameterExpression target = ExpressionParameter(typeof(object), "target");
Now the code compiles, but there is a problem at runtime.
I am trying to get the value of the Name of target property, which may make sense if the object was dynamic .
But since target considered to be of type object , the expression throws an error message indicating that Name does not exist as a property.
Is there an expression to retrieve a dynamic property?
Matias cicero
source share