diff --git a/src/Dapper.FluentMap/Utils/ReflectionHelper.cs b/src/Dapper.FluentMap/Utils/ReflectionHelper.cs index 832fd0e..d507e9d 100644 --- a/src/Dapper.FluentMap/Utils/ReflectionHelper.cs +++ b/src/Dapper.FluentMap/Utils/ReflectionHelper.cs @@ -1,4 +1,6 @@ -using System.Linq.Expressions; +using System; +using System.Linq; +using System.Linq.Expressions; using System.Reflection; namespace Dapper.FluentMap.Utils @@ -31,11 +33,22 @@ public static MemberInfo GetMemberInfo(LambdaExpression lambda) case ExpressionType.MemberAccess: var memberExpression = (MemberExpression)expr; var baseMember = memberExpression.Member; + Type paramType; + + while (memberExpression != null) + { + paramType = memberExpression.Type; + if (paramType.GetMembers().Any(member => member.Name == baseMember.Name)) + { + return paramType.GetMember(baseMember.Name)[0]; + } + + memberExpression = memberExpression.Expression as MemberExpression; + } // Make sure we get the property from the derived type. - var paramType = lambda.Parameters[0].Type; - var memberInfo = paramType.GetMember(baseMember.Name)[0]; - return memberInfo; + paramType = lambda.Parameters[0].Type; + return paramType.GetMember(baseMember.Name)[0]; default: return null; diff --git a/test/Dapper.FluentMap.Tests/Dapper.FluentMap.Tests.csproj b/test/Dapper.FluentMap.Tests/Dapper.FluentMap.Tests.csproj index 9211f44..50afc70 100644 --- a/test/Dapper.FluentMap.Tests/Dapper.FluentMap.Tests.csproj +++ b/test/Dapper.FluentMap.Tests/Dapper.FluentMap.Tests.csproj @@ -11,4 +11,7 @@ + + + \ No newline at end of file diff --git a/test/Dapper.FluentMap.Tests/ManualMappingTests.cs b/test/Dapper.FluentMap.Tests/ManualMappingTests.cs index b533207..5f82d7b 100644 --- a/test/Dapper.FluentMap.Tests/ManualMappingTests.cs +++ b/test/Dapper.FluentMap.Tests/ManualMappingTests.cs @@ -138,6 +138,16 @@ public void PropertyMapShouldMapInheritedProperies() Assert.Equal(typeof(DerivedTestEntity), nameMap.PropertyInfo.DeclaringType); } + [Fact] + public void PropertyMapShouldMapValueObjectProperties() + { + PreTest(); + + var map = new ValueObjectMap(); + var email = map.PropertyMaps.First(); + Assert.Equal(typeof(EmailTestValueObject), email.PropertyInfo.DeclaringType); + } + private static void PreTest() { FluentMapper.EntityMaps.Clear(); @@ -189,5 +199,13 @@ public MapWithDuplicateMapping() private class EmptyMap : EntityMap { } + + private class ValueObjectMap : EntityMap + { + public ValueObjectMap() + { + Map(x => x.Email.Address).ToColumn("email"); + } + } } } diff --git a/test/Dapper.FluentMap.Tests/TestEntity.cs b/test/Dapper.FluentMap.Tests/TestEntity.cs index 44bfd8c..2826780 100644 --- a/test/Dapper.FluentMap.Tests/TestEntity.cs +++ b/test/Dapper.FluentMap.Tests/TestEntity.cs @@ -9,4 +9,14 @@ public class DerivedTestEntity : TestEntity { public string Name { get; set; } } + + public class ValueObjectTestEntity + { + public EmailTestValueObject Email { get; set; } + } + + public class EmailTestValueObject + { + public string Address { get; set; } + } }