I've been trying to create an expression which can project a strongly typed EF Core entity into a dynamic object containing a list which are defined at runtime using a REST API call.
This is what I have so far:
Expression<Func<Message, dynamic>> DynamicFields(IEnumerable<string> fields)
{
var xParameter = Expression.Parameter(typeof(Message), "o");
var xNew = Expression.New(typeof(ExpandoObject));
var bindings = fields.Select(o => {
var mi = typeof(Message).GetProperty(o);
var xOriginal = Expression.Property(xParameter, mi);
return Expression.Bind(mi, xOriginal);
});
var xInit = Expression.MemberInit((dynamic)xNew, bindings);
return Expression.Lambda<Func<Message, dynamic>>(xInit, xParameter);
}
It feels like I'm super close, but this bombs out at runtime stating that X property is not a member of ExpandoObject. I've tried changing up the use of the dynamic and ExpandoObject, but nothing seems to work - is this even possible?
If I switch out dyanmic / ExpandoObject for Message, it works just fine, but returns an instance of the Message class with all of its properties at their default values.
Anyone done this before?
Cheers.
Aucun commentaire:
Enregistrer un commentaire