Casting Expression<Func<TEntity, TProperty>> to Expression<Func<TEntity, object>>
From time to time I’m dealing with API that’s using Expression
If you try to directly cast the expression, it will not work, of course. First I though, it’s going to be a lot of juggle with pieces of expression and reconstructing the final one. But it’s pretty easy, see yourself:
void FooBar<TEntity, TProperty>(TEntity entity, Expression<Func<TEntity, TProperty>> property)
{
Expression<Func<TEntity, object>> result;
if (typeof(TProperty).IsValueType)
result = Expression.Lambda<Func<TEntity, object>>(Expression.Convert(property.Body, typeof(object)), property.Parameters);
else
result = Expression.Lambda<Func<TEntity, object>>(property.Body, property.Parameters);
// do something with result ...
}
I’m simply creating new expression based on the original ones’ body and parameters. If the TProperty
’s type was value type I only do boxing in addition.
Nothing difficult, right?