@@ -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,23 +39,47 @@ 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 ;
47+ TypeName = GetTypeName ( normalizedType ) ;
48+ IsSourceEnum = isSourceEnum ;
4649
47- if ( SimpleTypes . TryGetValue ( normalizedType , out EntityPropertyTypeName typeName ) )
50+ if ( TypeName == EntityPropertyTypeName . Unknown )
4851 {
49- TypeName = typeName ;
52+ throw new NotSupportedException ( $ "{ source . PropertyType . FullName } is not supported.") ;
53+ }
54+ }
55+
56+ public static EntityPropertyTypeName GetTypeName ( Type type )
57+ {
58+ if ( SimpleTypes . TryGetValue ( type , out EntityPropertyTypeName typeName ) )
59+ {
60+ return typeName ;
5061 }
5162 else
5263 {
53- throw new NotSupportedException ( $ " { source . PropertyType . FullName } is not supported." ) ;
64+ return EntityPropertyTypeName . Unknown ;
5465 }
5566 }
5667
57- private 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 _ ) ;
5883
5984 public static bool IsSimpleType ( Type type )
6085 {
@@ -87,9 +112,9 @@ select g
87112
88113 foreach ( var sourceProperty in sourceProperties )
89114 {
90- var propertyType = GetNormalizedType ( sourceProperty . PropertyType ) ;
115+ var sourcePropertyNormalizedType = GetNormalizedType ( sourceProperty . PropertyType , out var isSourceEnum ) ;
91116
92- if ( targetPropertiesByType . TryGetValue ( propertyType , out Queue < PropertyInfo > ? targetProperties ) )
117+ if ( targetPropertiesByType . TryGetValue ( sourcePropertyNormalizedType , out Queue < PropertyInfo > ? targetProperties ) )
93118 {
94119 if ( targetProperties . Count == 0 )
95120 {
@@ -99,7 +124,8 @@ select g
99124 var mapping = new EntityPropertyMapping (
100125 sourceProperty ,
101126 targetProperties . Dequeue ( ) ,
102- propertyType
127+ sourcePropertyNormalizedType ,
128+ isSourceEnum
103129 ) ;
104130
105131 mappings . Add ( mapping ) ;
@@ -119,5 +145,38 @@ public static IReadOnlyList<EntityPropertyMapping> GetMappings<T>()
119145 {
120146 return GetMappings ( typeof ( T ) ) ;
121147 }
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+ }
122181 }
123182}
0 commit comments