@@ -17,6 +17,7 @@ internal sealed class EntityPropertyMapping
1717 public PropertyInfo Target { get ; }
1818 public Type NormalizedType { get ; }
1919 public EntityPropertyTypeName TypeName { get ; }
20+ public bool IsSourceEnum { get ; }
2021
2122 static EntityPropertyMapping ( )
2223 {
@@ -38,12 +39,13 @@ static EntityPropertyMapping()
3839 } ;
3940 }
4041
41- private EntityPropertyMapping ( PropertyInfo source , PropertyInfo target , Type normalizedType )
42+ private EntityPropertyMapping ( PropertyInfo source , PropertyInfo target , Type normalizedType , bool isSourceEnum )
4243 {
4344 Source = source ;
4445 Target = target ;
4546 NormalizedType = normalizedType ;
4647 TypeName = GetTypeName ( normalizedType ) ;
48+ IsSourceEnum = isSourceEnum ;
4749
4850 if ( TypeName == EntityPropertyTypeName . Unknown )
4951 {
@@ -63,7 +65,21 @@ public static EntityPropertyTypeName GetTypeName(Type type)
6365 }
6466 }
6567
66- public static Type GetNormalizedType ( Type type ) => Nullable . GetUnderlyingType ( type ) ?? type ;
68+ public static Type GetNormalizedType ( Type type , out bool isEnum )
69+ {
70+ type = Nullable . GetUnderlyingType ( type ) ?? type ;
71+
72+ isEnum = type . IsEnum ;
73+
74+ if ( isEnum )
75+ {
76+ type = Enum . GetUnderlyingType ( type ) ;
77+ }
78+
79+ return type ;
80+ }
81+
82+ public static Type GetNormalizedType ( Type type ) => GetNormalizedType ( type , out _ ) ;
6783
6884 public static bool IsSimpleType ( Type type )
6985 {
@@ -96,9 +112,9 @@ select g
96112
97113 foreach ( var sourceProperty in sourceProperties )
98114 {
99- var propertyType = GetNormalizedType ( sourceProperty . PropertyType ) ;
115+ var sourcePropertyNormalizedType = GetNormalizedType ( sourceProperty . PropertyType , out var isSourceEnum ) ;
100116
101- if ( targetPropertiesByType . TryGetValue ( propertyType , out Queue < PropertyInfo > ? targetProperties ) )
117+ if ( targetPropertiesByType . TryGetValue ( sourcePropertyNormalizedType , out Queue < PropertyInfo > ? targetProperties ) )
102118 {
103119 if ( targetProperties . Count == 0 )
104120 {
@@ -108,7 +124,8 @@ select g
108124 var mapping = new EntityPropertyMapping (
109125 sourceProperty ,
110126 targetProperties . Dequeue ( ) ,
111- propertyType
127+ sourcePropertyNormalizedType ,
128+ isSourceEnum
112129 ) ;
113130
114131 mappings . Add ( mapping ) ;
@@ -128,5 +145,38 @@ public static IReadOnlyList<EntityPropertyMapping> GetMappings<T>()
128145 {
129146 return GetMappings ( typeof ( T ) ) ;
130147 }
148+
149+ public object ? GetSourceNormalizedValue ( object objectInstance )
150+ {
151+ var value = Source . GetValue ( objectInstance ) ;
152+
153+ if ( value is null )
154+ {
155+ return null ;
156+ }
157+
158+ if ( IsSourceEnum )
159+ {
160+ switch ( TypeName )
161+ {
162+ case EntityPropertyTypeName . Int32 :
163+ value = ( int ) value ;
164+ break ;
165+ case EntityPropertyTypeName . Byte :
166+ value = ( byte ) value ;
167+ break ;
168+ case EntityPropertyTypeName . Int16 :
169+ value = ( short ) value ;
170+ break ;
171+ case EntityPropertyTypeName . Int64 :
172+ value = ( long ) value ;
173+ break ;
174+ default :
175+ throw new NotSupportedException ( $ "The underlying type of { NormalizedType . FullName } ({ Enum . GetUnderlyingType ( NormalizedType ) . FullName } ) is not supported.") ;
176+ }
177+ }
178+
179+ return value ;
180+ }
131181 }
132182}
0 commit comments