Dapper-FluentMap icon indicating copy to clipboard operation
Dapper-FluentMap copied to clipboard

Throws InvalidCastException when a string property has a name that conflicts with the String Type built in function members

Open damianos-pappas opened this issue 4 years ago • 2 comments

I have an Entity with a property named "Format". When mapping, FluentMap throws:

InvalidCastException: Unable to cast object of type 'System.Reflection.RuntimeMethodInfo' to type 'System.Reflection.PropertyInfo'. Dapper.FluentMap.Mapping.EntityMapBase<TEntity, TPropertyMap>.Map(Expression<Func<TEntity, object>> expression)

This is caused probably because in the ReflectionHelper.cs in line 70 you have the following code:

return paramType.GetMember(member.Name)[0]

It takes the first Member with the name given. I believe that if you change it so that it uses only Property members like below, the problem will be solved:

return paramType.GetMember(member.Name).Where(a=> a.MemberType == MemberTypes.Property).First();

Thanks!

damianos-pappas avatar May 22 '20 08:05 damianos-pappas

Would you like to send A PR?

henkmollema avatar May 28 '20 19:05 henkmollema

Same problem exists with other types. For example, mapping TimeSpan Duration { get; set; } property fails with same error message, I guess because TimeSpan type has Duration() function.

dusansmail avatar Jun 29 '21 15:06 dusansmail