verdie-g/StoredProcedureEFCore

Use Expression.TypeAs when !PropertyInfo.IsValueType

verdie-g opened this issue · 1 comments

Use Expression.TypeAs when !PropertyInfo.IsValueType

In Mapper.MapColumnsToProperties

ParameterExpression instance = Expression.Parameter(typeof(object), "instance");
ParameterExpression value = Expression.Parameter(typeof(object), "value");

// "x as T" is faster than "(T) x", so use that if not a value type
UnaryExpression instanceCast = prop.DeclaringType.IsValueType
    ? Expression.Convert(instance, prop.DeclaringType)
    : Expression.TypeAs(instance, prop.DeclaringType);

UnaryExpression valueCast = prop.PropertyType.IsValueType
    ? Expression.Convert(value, prop.PropertyType)
    : Expression.TypeAs(value, prop.PropertyType);

MethodCallExpression setterCall = Expression.Call(instanceCast, prop.GetSetMethod(), valueCast);
var setter = (Action<object, object>) Expression.Lambda(setterCall, instance, value).Compile();