@@ -34,6 +34,25 @@ public static Expression<FuncSpanArrayT<T>> RecordParserSpanCSV<T>(IEnumerable<M
34
34
return result ;
35
35
}
36
36
37
+ public static Expression < Func < Foo , T > > RecordParserSpanFlatAOT < T > ( IEnumerable < MappingReadConfiguration > mappedColumns , Func < T > factory )
38
+ {
39
+ // parameters
40
+ var line = Expression . Parameter ( typeof ( Foo ) , "span" ) ;
41
+
42
+ // variables
43
+ var instanceVariable = Expression . Variable ( typeof ( T ) , "inst" ) ;
44
+
45
+ var blockThatSetProperties = MountSetProperties ( instanceVariable , mappedColumns , ( i , mapConfig ) =>
46
+ {
47
+ return Slice ( line , Expression . Constant ( mapConfig . start ) , Expression . Constant ( mapConfig . length . Value ) ) ;
48
+ } , true ) ;
49
+
50
+ var body = MountBody ( instanceVariable , blockThatSetProperties , mappedColumns , factory ) ;
51
+ var result = Expression . Lambda < Func < Foo , T > > ( body , line ) ;
52
+
53
+ return result ;
54
+ }
55
+
37
56
public static Expression < FuncSpanT < T > > RecordParserSpanFlat < T > ( IEnumerable < MappingReadConfiguration > mappedColumns , Func < T > factory )
38
57
{
39
58
// parameters
@@ -54,9 +73,9 @@ public static Expression<FuncSpanT<T>> RecordParserSpanFlat<T>(IEnumerable<Mappi
54
73
}
55
74
56
75
private static BlockExpression MountBody < T > (
57
- ParameterExpression instanceVariable ,
58
- BlockExpression blockThatSetProperties ,
59
- IEnumerable < MappingReadConfiguration > mappedColumns ,
76
+ ParameterExpression instanceVariable ,
77
+ BlockExpression blockThatSetProperties ,
78
+ IEnumerable < MappingReadConfiguration > mappedColumns ,
60
79
Func < T > factory )
61
80
{
62
81
var getNewInstance = factory != null
@@ -75,12 +94,15 @@ private static BlockExpression MountBody<T>(
75
94
private static BlockExpression MountSetProperties (
76
95
ParameterExpression objectParameter ,
77
96
IEnumerable < MappingReadConfiguration > mappedColumns ,
78
- Func < int , MappingReadConfiguration , Expression > getTextValue )
97
+ Func < int , MappingReadConfiguration , Expression > getTextValue ,
98
+ bool AOT = false )
79
99
{
80
100
var replacer = new ParameterReplacerVisitor ( objectParameter ) ;
81
101
var assignsExpressions = new List < Expression > ( ) ;
82
102
var i = - 1 ;
83
103
104
+ Func < Type , Expression , Delegate , Expression > resolve = AOT ? GetValueToBeSetExpressionAOT : GetValueToBeSetExpression ;
105
+
84
106
foreach ( var x in mappedColumns )
85
107
{
86
108
i ++ ;
@@ -95,7 +117,7 @@ private static BlockExpression MountSetProperties(
95
117
var isPropertyNullable = nullableUnderlyingType != null ;
96
118
var propertyUnderlyingType = nullableUnderlyingType ?? propertyType ;
97
119
98
- Expression valueToBeSetExpression = GetValueToBeSetExpression (
120
+ Expression valueToBeSetExpression = resolve (
99
121
propertyUnderlyingType ,
100
122
textValue ,
101
123
x . fmask ) ;
@@ -137,5 +159,28 @@ private static Expression GetValueToBeSetExpression(Type propertyType, Expressio
137
159
138
160
throw new InvalidOperationException ( $ "Type '{ propertyType . FullName } ' does not have a default parse") ;
139
161
}
162
+
163
+ private static Expression GetValueToBeSetExpressionAOT ( Type propertyType , Expression valueText , Delegate func )
164
+ {
165
+ if ( func != null )
166
+ {
167
+ var fun = Expression . Constant ( func ) ;
168
+
169
+ return Expression . Call ( valueText , nameof ( Foo . AsCustom ) , new [ ] { func . Method . ReturnType } , fun ) ;
170
+ }
171
+
172
+ if ( propertyType . IsEnum )
173
+ {
174
+ return Expression . Call ( valueText , nameof ( Foo . AsEnum ) , new [ ] { propertyType } ) ;
175
+ }
176
+
177
+ var methodName = "As" + propertyType . Name ;
178
+ var method = typeof ( Foo ) . GetMethod ( methodName ) ;
179
+
180
+ if ( method == null )
181
+ throw new InvalidOperationException ( $ "Type '{ propertyType . FullName } ' does not have a default parse") ;
182
+
183
+ return Expression . Call ( valueText , method ) ;
184
+ }
140
185
}
141
186
}
0 commit comments