I’m working with API that want expressions as parameters to identitfy/modify properties on an object. This works fine if I know the type at compile Time. E.g. the APi requires an Expression<Func<T, object>>
and I can use it using an expression like x => x.Id
But, in a generic world, I have an object obj to work with and I know the name of the property. How do I build the express Expression<Func<T, object>>
?
Likewise, the API I’m dealing with also needs an expression to set a given property on an object to a given value.
The API looks like this:
void Patch<T, U>(string id, Expression<Func<T, TProperty>> fieldPath, TProperty value)
when I know T and how the object looks like, I can
Patch<T, TProperty>("some_id", x => x.Id, someValue);
But, if I don’t know T and TProperty at compile time, I need to formulate the proper expression.
Given the APIs I’m working with, I cannot use PropertyInfo.GetValue/SetValue
(for which I do have a solution)