@@ -131,6 +131,14 @@ public static IEnumerable<Instruction> PushConvertedValue(this ValueNode node, I
131
131
unboxValueTypes ) ;
132
132
}
133
133
134
+ static T TryFormat < T > ( Func < string , T > func , IXmlLineInfo lineInfo , string str )
135
+ {
136
+ try {
137
+ return func ( str ) ;
138
+ } catch ( FormatException fex ) {
139
+ throw new BuildException ( BuildExceptionCode . Conversion , lineInfo , fex , str , typeof ( T ) ) ;
140
+ }
141
+ }
134
142
public static IEnumerable < Instruction > PushConvertedValue ( this ValueNode node , ILContext context ,
135
143
TypeReference targetTypeRef , TypeReference typeConverter , IEnumerable < Instruction > pushServiceProvider ,
136
144
bool boxValueTypes , bool unboxValueTypes )
@@ -208,39 +216,39 @@ public static IEnumerable<Instruction> PushConvertedValue(this ValueNode node, I
208
216
if ( targetTypeRef . ResolveCached ( ) . BaseType != null && targetTypeRef . ResolveCached ( ) . BaseType . FullName == "System.Enum" )
209
217
yield return PushParsedEnum ( targetTypeRef , str , node ) ;
210
218
else if ( targetTypeRef . FullName == "System.Char" )
211
- yield return Instruction . Create ( OpCodes . Ldc_I4 , unchecked ( ( int ) Char . Parse ( str ) ) ) ;
219
+ yield return Instruction . Create ( OpCodes . Ldc_I4 , unchecked ( ( int ) TryFormat ( Char . Parse , node , str ) ) ) ;
212
220
else if ( targetTypeRef . FullName == "System.SByte" )
213
- yield return Instruction . Create ( OpCodes . Ldc_I4 , unchecked ( ( int ) SByte . Parse ( str , CultureInfo . InvariantCulture ) ) ) ;
221
+ yield return Instruction . Create ( OpCodes . Ldc_I4 , unchecked ( ( int ) TryFormat ( s => SByte . Parse ( s , CultureInfo . InvariantCulture ) , node , str ) ) ) ;
214
222
else if ( targetTypeRef . FullName == "System.Int16" )
215
- yield return Instruction . Create ( OpCodes . Ldc_I4 , unchecked ( ( int ) Int16 . Parse ( str , CultureInfo . InvariantCulture ) ) ) ;
223
+ yield return Instruction . Create ( OpCodes . Ldc_I4 , unchecked ( ( int ) TryFormat ( s => Int16 . Parse ( s , CultureInfo . InvariantCulture ) , node , str ) ) ) ;
216
224
else if ( targetTypeRef . FullName == "System.Int32" )
217
- yield return Instruction . Create ( OpCodes . Ldc_I4 , Int32 . Parse ( str , CultureInfo . InvariantCulture ) ) ;
225
+ yield return Instruction . Create ( OpCodes . Ldc_I4 , TryFormat ( s => Int32 . Parse ( s , CultureInfo . InvariantCulture ) , node , str ) ) ;
218
226
else if ( targetTypeRef . FullName == "System.Int64" )
219
- yield return Instruction . Create ( OpCodes . Ldc_I8 , Int64 . Parse ( str , CultureInfo . InvariantCulture ) ) ;
227
+ yield return Instruction . Create ( OpCodes . Ldc_I8 , TryFormat ( s => Int64 . Parse ( s , CultureInfo . InvariantCulture ) , node , str ) ) ;
220
228
else if ( targetTypeRef . FullName == "System.Byte" )
221
- yield return Instruction . Create ( OpCodes . Ldc_I4 , unchecked ( ( int ) Byte . Parse ( str , CultureInfo . InvariantCulture ) ) ) ;
229
+ yield return Instruction . Create ( OpCodes . Ldc_I4 , unchecked ( ( int ) TryFormat ( s => Byte . Parse ( s , CultureInfo . InvariantCulture ) , node , str ) ) ) ;
222
230
else if ( targetTypeRef . FullName == "System.UInt16" )
223
- yield return Instruction . Create ( OpCodes . Ldc_I4 , unchecked ( ( int ) UInt16 . Parse ( str , CultureInfo . InvariantCulture ) ) ) ;
231
+ yield return Instruction . Create ( OpCodes . Ldc_I4 , unchecked ( ( int ) TryFormat ( s => UInt16 . Parse ( s , CultureInfo . InvariantCulture ) , node , str ) ) ) ;
224
232
else if ( targetTypeRef . FullName == "System.UInt32" )
225
- yield return Instruction . Create ( OpCodes . Ldc_I4 , unchecked ( ( int ) UInt32 . Parse ( str , CultureInfo . InvariantCulture ) ) ) ;
233
+ yield return Instruction . Create ( OpCodes . Ldc_I4 , unchecked ( ( int ) TryFormat ( s => UInt32 . Parse ( s , CultureInfo . InvariantCulture ) , node , str ) ) ) ;
226
234
else if ( targetTypeRef . FullName == "System.UInt64" )
227
- yield return Instruction . Create ( OpCodes . Ldc_I8 , unchecked ( ( long ) UInt64 . Parse ( str , CultureInfo . InvariantCulture ) ) ) ;
235
+ yield return Instruction . Create ( OpCodes . Ldc_I8 , unchecked ( ( long ) TryFormat ( s => UInt64 . Parse ( s , CultureInfo . InvariantCulture ) , node , str ) ) ) ;
228
236
else if ( targetTypeRef . FullName == "System.Single" )
229
- yield return Instruction . Create ( OpCodes . Ldc_R4 , Single . Parse ( str , CultureInfo . InvariantCulture ) ) ;
237
+ yield return Instruction . Create ( OpCodes . Ldc_R4 , TryFormat ( s => Single . Parse ( str , CultureInfo . InvariantCulture ) , node , str ) ) ;
230
238
else if ( targetTypeRef . FullName == "System.Double" )
231
- yield return Instruction . Create ( OpCodes . Ldc_R8 , Double . Parse ( str , CultureInfo . InvariantCulture ) ) ;
239
+ yield return Instruction . Create ( OpCodes . Ldc_R8 , TryFormat ( s => Double . Parse ( str , CultureInfo . InvariantCulture ) , node , str ) ) ;
232
240
else if ( targetTypeRef . FullName == "System.Boolean" ) {
233
- if ( Boolean . Parse ( str ) )
241
+ if ( TryFormat ( Boolean . Parse , node , str ) )
234
242
yield return Instruction . Create ( OpCodes . Ldc_I4_1 ) ;
235
243
else
236
244
yield return Instruction . Create ( OpCodes . Ldc_I4_0 ) ;
237
245
} else if ( targetTypeRef . FullName == "System.TimeSpan" ) {
238
- var ts = TimeSpan . Parse ( str , CultureInfo . InvariantCulture ) ;
246
+ var ts = TryFormat ( s => TimeSpan . Parse ( s , CultureInfo . InvariantCulture ) , node , str ) ;
239
247
var ticks = ts . Ticks ;
240
248
yield return Instruction . Create ( OpCodes . Ldc_I8 , ticks ) ;
241
249
yield return Instruction . Create ( OpCodes . Newobj , module . ImportCtorReference ( ( "mscorlib" , "System" , "TimeSpan" ) , parameterTypes : new [ ] { ( "mscorlib" , "System" , "Int64" ) } ) ) ;
242
250
} else if ( targetTypeRef . FullName == "System.DateTime" ) {
243
- var dt = DateTime . Parse ( str , CultureInfo . InvariantCulture ) ;
251
+ var dt = TryFormat ( s => DateTime . Parse ( s , CultureInfo . InvariantCulture ) , node , str ) ;
244
252
var ticks = dt . Ticks ;
245
253
yield return Instruction . Create ( OpCodes . Ldc_I8 , ticks ) ;
246
254
yield return Instruction . Create ( OpCodes . Newobj , module . ImportCtorReference ( ( "mscorlib" , "System" , "DateTime" ) , parameterTypes : new [ ] { ( "mscorlib" , "System" , "Int64" ) } ) ) ;
0 commit comments