henkmollema / Dapper-FluentMap

Provides a simple API to fluently map POCO properties to database columns when using Dapper.
MIT License
427 stars 86 forks source link

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

Closed damianos-pappas closed 1 year ago

damianos-pappas commented 4 years ago

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!

henkmollema commented 4 years ago

Would you like to send A PR?

dusansmail commented 3 years ago

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.