Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Expression.TypeAs when !PropertyInfo.IsValueType #29

Closed
verdie-g opened this issue Jan 3, 2020 · 1 comment
Closed

Use Expression.TypeAs when !PropertyInfo.IsValueType #29

verdie-g opened this issue Jan 3, 2020 · 1 comment
Assignees

Comments

@verdie-g
Copy link
Owner

verdie-g commented Jan 3, 2020

No description provided.

@verdie-g verdie-g self-assigned this Jan 3, 2020
@verdie-g
Copy link
Owner Author

verdie-g commented Jan 3, 2020

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();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant