1
- // Copyright (c) 2018-2019 , Yves Goergen, https://unclassified.software
1
+ // Copyright (c) 2018-2020 , Yves Goergen, https://unclassified.software
2
2
//
3
3
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
4
4
// associated documentation files (the "Software"), to deal in the Software without restriction,
@@ -63,7 +63,7 @@ public static partial class DeepConvert
63
63
/// <summary>
64
64
/// The UNIX epoch time.
65
65
/// </summary>
66
- private static DateTime unixEpoch = new DateTime ( 1970 , 1 , 1 , 0 , 0 , 0 , DateTimeKind . Utc ) ;
66
+ private static readonly DateTime unixEpoch = new DateTime ( 1970 , 1 , 1 , 0 , 0 , 0 , DateTimeKind . Utc ) ;
67
67
68
68
#endregion Private data
69
69
@@ -139,7 +139,7 @@ public static object ChangeType(object value, Type destType, DeepConvertSettings
139
139
if ( Nullable . GetUnderlyingType ( destType ) != null )
140
140
{
141
141
// Special handling for empty strings
142
- if ( value is string && ( string ) value == "" )
142
+ if ( value is string str && str == "" )
143
143
return null ;
144
144
145
145
return ChangeType ( value , Nullable . GetUnderlyingType ( destType ) , settings ) ;
@@ -188,103 +188,103 @@ public static object ChangeType(object value, Type destType, DeepConvertSettings
188
188
{
189
189
if ( destType == typeof ( bool ) ) return ! falseWords . Any ( w => string . Equals ( w , ( string ) value , StringComparison . OrdinalIgnoreCase ) ) ;
190
190
if ( destType == typeof ( BigInteger ) ) return BigInteger . Parse ( ( string ) value , provider ) ;
191
- if ( destType == typeof ( DateTime ) ) return ToDateTime ( value , settings ) ; //DateTime.Parse((string)value, provider);
191
+ if ( destType == typeof ( DateTime ) ) return ToDateTime ( value , settings ) ;
192
192
if ( destType == typeof ( TimeSpan ) ) return TimeSpan . Parse ( ( string ) value , provider ) ;
193
193
if ( destType == typeof ( Guid ) ) return Guid . Parse ( ( string ) value ) ;
194
194
}
195
195
if ( srcType == typeof ( char ) )
196
196
{
197
- if ( destType == typeof ( bool ) ) return ! value . Equals ( ( char ) 0 ) ? true : false ;
197
+ if ( destType == typeof ( bool ) ) return ! value . Equals ( ( char ) 0 ) ;
198
198
if ( destType == typeof ( BigInteger ) ) return new BigInteger ( ( char ) value ) ;
199
- if ( destType == typeof ( DateTime ) ) return ToDateTime ( value , settings ) ; //new DateTime((char)value);
199
+ if ( destType == typeof ( DateTime ) ) return ToDateTime ( value , settings ) ;
200
200
if ( destType == typeof ( TimeSpan ) ) return new TimeSpan ( ( char ) value ) ;
201
201
// No conversion to Guid
202
202
}
203
203
if ( srcType == typeof ( byte ) )
204
204
{
205
- if ( destType == typeof ( bool ) ) return ! value . Equals ( ( byte ) 0 ) ? true : false ;
205
+ if ( destType == typeof ( bool ) ) return ! value . Equals ( ( byte ) 0 ) ;
206
206
if ( destType == typeof ( BigInteger ) ) return new BigInteger ( ( byte ) value ) ;
207
- if ( destType == typeof ( DateTime ) ) return ToDateTime ( value , settings ) ; //new DateTime((byte)value);
207
+ if ( destType == typeof ( DateTime ) ) return ToDateTime ( value , settings ) ;
208
208
if ( destType == typeof ( TimeSpan ) ) return new TimeSpan ( ( byte ) value ) ;
209
209
// No conversion to Guid
210
210
}
211
211
if ( srcType == typeof ( sbyte ) )
212
212
{
213
- if ( destType == typeof ( bool ) ) return ! value . Equals ( ( sbyte ) 0 ) ? true : false ;
213
+ if ( destType == typeof ( bool ) ) return ! value . Equals ( ( sbyte ) 0 ) ;
214
214
if ( destType == typeof ( BigInteger ) ) return new BigInteger ( ( sbyte ) value ) ;
215
- if ( destType == typeof ( DateTime ) ) return ToDateTime ( value , settings ) ; //new DateTime((sbyte)value);
215
+ if ( destType == typeof ( DateTime ) ) return ToDateTime ( value , settings ) ;
216
216
if ( destType == typeof ( TimeSpan ) ) return new TimeSpan ( ( sbyte ) value ) ;
217
217
// No conversion to Guid
218
218
}
219
219
if ( srcType == typeof ( short ) )
220
220
{
221
- if ( destType == typeof ( bool ) ) return ! value . Equals ( ( short ) 0 ) ? true : false ;
221
+ if ( destType == typeof ( bool ) ) return ! value . Equals ( ( short ) 0 ) ;
222
222
if ( destType == typeof ( BigInteger ) ) return new BigInteger ( ( short ) value ) ;
223
- if ( destType == typeof ( DateTime ) ) return ToDateTime ( value , settings ) ; //new DateTime((short)value);
223
+ if ( destType == typeof ( DateTime ) ) return ToDateTime ( value , settings ) ;
224
224
if ( destType == typeof ( TimeSpan ) ) return new TimeSpan ( ( short ) value ) ;
225
225
// No conversion to Guid
226
226
}
227
227
if ( srcType == typeof ( ushort ) )
228
228
{
229
- if ( destType == typeof ( bool ) ) return ! value . Equals ( ( ushort ) 0 ) ? true : false ;
229
+ if ( destType == typeof ( bool ) ) return ! value . Equals ( ( ushort ) 0 ) ;
230
230
if ( destType == typeof ( BigInteger ) ) return new BigInteger ( ( ushort ) value ) ;
231
- if ( destType == typeof ( DateTime ) ) return ToDateTime ( value , settings ) ; //new DateTime((ushort)value);
231
+ if ( destType == typeof ( DateTime ) ) return ToDateTime ( value , settings ) ;
232
232
if ( destType == typeof ( TimeSpan ) ) return new TimeSpan ( ( ushort ) value ) ;
233
233
// No conversion to Guid
234
234
}
235
235
if ( srcType == typeof ( int ) )
236
236
{
237
- if ( destType == typeof ( bool ) ) return ! value . Equals ( 0 ) ? true : false ;
237
+ if ( destType == typeof ( bool ) ) return ! value . Equals ( 0 ) ;
238
238
if ( destType == typeof ( BigInteger ) ) return new BigInteger ( ( int ) value ) ;
239
- if ( destType == typeof ( DateTime ) ) return ToDateTime ( value , settings ) ; //new DateTime((int)value);
239
+ if ( destType == typeof ( DateTime ) ) return ToDateTime ( value , settings ) ;
240
240
if ( destType == typeof ( TimeSpan ) ) return new TimeSpan ( ( int ) value ) ;
241
241
// No conversion to Guid
242
242
}
243
243
if ( srcType == typeof ( uint ) )
244
244
{
245
- if ( destType == typeof ( bool ) ) return ! value . Equals ( ( uint ) 0 ) ? true : false ;
245
+ if ( destType == typeof ( bool ) ) return ! value . Equals ( ( uint ) 0 ) ;
246
246
if ( destType == typeof ( BigInteger ) ) return new BigInteger ( ( uint ) value ) ;
247
- if ( destType == typeof ( DateTime ) ) return ToDateTime ( value , settings ) ; //new DateTime((uint)value);
247
+ if ( destType == typeof ( DateTime ) ) return ToDateTime ( value , settings ) ;
248
248
if ( destType == typeof ( TimeSpan ) ) return new TimeSpan ( ( uint ) value ) ;
249
249
// No conversion to Guid
250
250
}
251
251
if ( srcType == typeof ( long ) )
252
252
{
253
- if ( destType == typeof ( bool ) ) return ! value . Equals ( ( long ) 0 ) ? true : false ;
253
+ if ( destType == typeof ( bool ) ) return ! value . Equals ( ( long ) 0 ) ;
254
254
if ( destType == typeof ( BigInteger ) ) return new BigInteger ( ( long ) value ) ;
255
- if ( destType == typeof ( DateTime ) ) return ToDateTime ( value , settings ) ; //new DateTime((long)value);
255
+ if ( destType == typeof ( DateTime ) ) return ToDateTime ( value , settings ) ;
256
256
if ( destType == typeof ( TimeSpan ) ) return new TimeSpan ( ( long ) value ) ;
257
257
// No conversion to Guid
258
258
}
259
259
if ( srcType == typeof ( ulong ) )
260
260
{
261
- if ( destType == typeof ( bool ) ) return ! value . Equals ( ( ulong ) 0 ) ? true : false ;
261
+ if ( destType == typeof ( bool ) ) return ! value . Equals ( ( ulong ) 0 ) ;
262
262
if ( destType == typeof ( BigInteger ) ) return new BigInteger ( ( ulong ) value ) ;
263
- if ( destType == typeof ( DateTime ) ) return ToDateTime ( value , settings ) ; //new DateTime((long)(ulong)value);
263
+ if ( destType == typeof ( DateTime ) ) return ToDateTime ( value , settings ) ;
264
264
if ( destType == typeof ( TimeSpan ) ) return new TimeSpan ( ( long ) ( ulong ) value ) ;
265
265
// No conversion to Guid
266
266
}
267
267
if ( srcType == typeof ( float ) )
268
268
{
269
- if ( destType == typeof ( bool ) ) return ! value . Equals ( ( float ) 0 ) ? true : false ;
269
+ if ( destType == typeof ( bool ) ) return ! value . Equals ( ( float ) 0 ) ;
270
270
if ( destType == typeof ( BigInteger ) ) return new BigInteger ( ( float ) value ) ;
271
- if ( destType == typeof ( DateTime ) ) return ToDateTime ( value , settings ) ; //new DateTime((long)(float)value);
271
+ if ( destType == typeof ( DateTime ) ) return ToDateTime ( value , settings ) ;
272
272
if ( destType == typeof ( TimeSpan ) ) return new TimeSpan ( ( long ) ( float ) value ) ;
273
273
// No conversion to Guid
274
274
}
275
275
if ( srcType == typeof ( double ) )
276
276
{
277
- if ( destType == typeof ( bool ) ) return ! value . Equals ( ( double ) 0 ) ? true : false ;
277
+ if ( destType == typeof ( bool ) ) return ! value . Equals ( ( double ) 0 ) ;
278
278
if ( destType == typeof ( BigInteger ) ) return new BigInteger ( ( double ) value ) ;
279
- if ( destType == typeof ( DateTime ) ) return ToDateTime ( value , settings ) ; //new DateTime((long)(double)value);
279
+ if ( destType == typeof ( DateTime ) ) return ToDateTime ( value , settings ) ;
280
280
if ( destType == typeof ( TimeSpan ) ) return new TimeSpan ( ( long ) ( double ) value ) ;
281
281
// No conversion to Guid
282
282
}
283
283
if ( srcType == typeof ( decimal ) )
284
284
{
285
- if ( destType == typeof ( bool ) ) return ! value . Equals ( ( decimal ) 0 ) ? true : false ;
285
+ if ( destType == typeof ( bool ) ) return ! value . Equals ( ( decimal ) 0 ) ;
286
286
if ( destType == typeof ( BigInteger ) ) return new BigInteger ( ( decimal ) value ) ;
287
- if ( destType == typeof ( DateTime ) ) return ToDateTime ( value , settings ) ; //new DateTime((long)(decimal)value);
287
+ if ( destType == typeof ( DateTime ) ) return ToDateTime ( value , settings ) ;
288
288
if ( destType == typeof ( TimeSpan ) ) return new TimeSpan ( ( long ) ( decimal ) value ) ;
289
289
// No conversion to Guid
290
290
}
@@ -306,9 +306,9 @@ public static object ChangeType(object value, Type destType, DeepConvertSettings
306
306
object enumNumericValue = Convert . ChangeType ( enumValue , Enum . GetUnderlyingType ( destType ) , provider ) ;
307
307
return Enum . ToObject ( destType , enumNumericValue ) ;
308
308
}
309
- if ( destType == typeof ( bool ) ) return ! enumValue . Equals ( 0 ) ? true : false ;
309
+ if ( destType == typeof ( bool ) ) return ! enumValue . Equals ( 0 ) ;
310
310
if ( destType == typeof ( BigInteger ) ) return new BigInteger ( Convert . ToInt64 ( enumValue ) ) ;
311
- if ( destType == typeof ( DateTime ) ) return ToDateTime ( value , settings ) ; //new DateTime(Convert.ToInt64(enumValue));
311
+ if ( destType == typeof ( DateTime ) ) return ToDateTime ( value , settings ) ;
312
312
if ( destType == typeof ( TimeSpan ) ) return new TimeSpan ( Convert . ToInt64 ( enumValue ) ) ;
313
313
// No conversion to Guid
314
314
}
@@ -354,9 +354,9 @@ public static object ChangeType(object value, Type destType, DeepConvertSettings
354
354
object enumNumericValue = Convert . ChangeType ( ( ( BigInteger ) value ) . ToString ( ) , Enum . GetUnderlyingType ( destType ) , provider ) ;
355
355
return Enum . ToObject ( destType , enumNumericValue ) ;
356
356
}
357
- if ( destType == typeof ( bool ) ) return ! ( ( BigInteger ) value ) . IsZero ? true : false ;
357
+ if ( destType == typeof ( bool ) ) return ! ( ( BigInteger ) value ) . IsZero ;
358
358
if ( destType == typeof ( BigInteger ) ) return value ;
359
- if ( destType == typeof ( DateTime ) ) return ToDateTime ( value , settings ) ; //new DateTime(Convert.ToInt64(((BigInteger)value).ToString(), provider));
359
+ if ( destType == typeof ( DateTime ) ) return ToDateTime ( value , settings ) ;
360
360
if ( destType == typeof ( TimeSpan ) ) return new TimeSpan ( Convert . ToInt64 ( ( ( BigInteger ) value ) . ToString ( ) , provider ) ) ;
361
361
// No conversion to Guid
362
362
}
@@ -392,9 +392,9 @@ public static object ChangeType(object value, Type destType, DeepConvertSettings
392
392
throw new ArgumentException ( "Unknown date numeric kind." ) ;
393
393
}
394
394
}
395
- if ( destType == typeof ( bool ) ) return ( ( DateTime ) value ) . Ticks != 0 ? true : false ;
395
+ if ( destType == typeof ( bool ) ) return ( ( DateTime ) value ) . Ticks != 0 ;
396
396
if ( destType == typeof ( DateTime ) ) return value ;
397
- if ( destType == typeof ( TimeSpan ) ) return ( ( DateTime ) value ) . TimeOfDay ; //new TimeSpan(((DateTime)value).Ticks);
397
+ if ( destType == typeof ( TimeSpan ) ) return ( ( DateTime ) value ) . TimeOfDay ;
398
398
// No conversion to Guid
399
399
}
400
400
if ( srcType == typeof ( TimeSpan ) )
@@ -430,8 +430,8 @@ public static object ChangeType(object value, Type destType, DeepConvertSettings
430
430
}
431
431
//return Convert.ChangeType(((TimeSpan)value).Ticks, destType);
432
432
}
433
- if ( destType == typeof ( bool ) ) return ( ( TimeSpan ) value ) . Ticks != 0 ? true : false ;
434
- if ( destType == typeof ( DateTime ) ) return ToDateTime ( value , settings ) ; //new DateTime(((TimeSpan)value).Ticks);
433
+ if ( destType == typeof ( bool ) ) return ( ( TimeSpan ) value ) . Ticks != 0 ;
434
+ if ( destType == typeof ( DateTime ) ) return ToDateTime ( value , settings ) ;
435
435
if ( destType == typeof ( TimeSpan ) ) return value ;
436
436
// No conversion to Guid
437
437
}
0 commit comments