@@ -327,14 +327,33 @@ private static void LoadFieldsForObject(object objObject, System.Type objType, S
327327 {
328328 try
329329 {
330+ var nullableBaseType = GetNullableBaseType ( objField . FieldType ) ;
331+
330332 //If an enum field then convert the integer to the enum equivalent
331- if ( objField . FieldType . IsEnum )
333+ if ( nullableBaseType != null && nullableBaseType . IsEnum )
334+ {
335+ var value = objFields [ objFieldMapping . FieldName ] . Value ;
336+ if ( value != null )
337+ value = System . Enum . ToObject ( objField . FieldType . GetGenericArguments ( ) [ 0 ] , value ) ;
338+
339+ objField . SetValue ( objObject , value ) ;
340+ }
341+ else if ( objField . FieldType . IsEnum )
332342 objField . SetValue ( objObject , System . Enum . ToObject ( objField . FieldType , objFields [ objFieldMapping . FieldName ] . Value ) ) ;
333- else if ( objField . GetValue ( objObject ) is ObjectReference ) //Set the distinct value if this is an ObjectReference field
334- ( ( ObjectReference ) ( objField . GetValue ( objObject ) ) ) . DistinctValue = objFields [ objFieldMapping . FieldName ] . Value ;
335- else if ( objField . FieldType . Equals ( typeof ( bool ) ) && ! objFields [ objFieldMapping . FieldName ] . Value . GetType ( ) . Equals ( typeof ( bool ) ) )
336- //MySQL connection provides BIT data as a ULong data type, so convert to boolean.
337- objField . SetValue ( objObject , System . Convert . ToInt32 ( objFields [ objFieldMapping . FieldName ] . Value ) != 0 ) ;
343+ else if ( objField . GetValue ( objObject ) is ObjectReference ) //Set the distinct value if this is an ObjectReference field
344+ ( ( ObjectReference ) ( objField . GetValue ( objObject ) ) ) . DistinctValue = objFields [ objFieldMapping . FieldName ] . Value ;
345+ else if (
346+ ( objField . FieldType . Equals ( typeof ( bool ) ) || ( nullableBaseType != null && nullableBaseType . Equals ( typeof ( bool ) ) ) ) &&
347+ ! objFields [ objFieldMapping . FieldName ] . Value . GetType ( ) . Equals ( typeof ( bool ) ) )
348+ {
349+ var value = objFields [ objFieldMapping . FieldName ] . Value ;
350+
351+ if ( value != null )
352+ value = System . Convert . ToInt32 ( value ) != 0 ;
353+
354+ // MySQL connection provides BIT data as a ULong data type, so convert to boolean.
355+ objField . SetValue ( objObject , value ) ;
356+ }
338357 else
339358 objField . SetValue ( objObject , objFields [ objFieldMapping . FieldName ] . Value ) ;
340359 }
@@ -362,19 +381,39 @@ private static void LoadFieldsForObject(object objObject, System.Type objType, S
362381 {
363382 try
364383 {
365- //If an enum field then convert the integer to the enum equivalent
366- if ( objProperty . PropertyType . IsEnum )
384+ var nullableBaseType = GetNullableBaseType ( objProperty . PropertyType ) ;
385+
386+ if ( nullableBaseType != null && nullableBaseType . IsEnum )
387+ {
388+ var value = objFields [ objFieldMapping . FieldName ] . Value ;
389+ if ( value != null )
390+ value = System . Enum . ToObject ( objProperty . PropertyType . GetGenericArguments ( ) [ 0 ] , value ) ;
391+
392+ objProperty . SetValue ( objObject , value , null ) ;
393+ }
394+ // If an enum field then convert the integer to the enum equivalent
395+ else if ( objProperty . PropertyType . IsEnum )
367396 {
368397 objProperty . SetValue ( objObject , System . Enum . ToObject ( objProperty . PropertyType , objFields [ objFieldMapping . FieldName ] . Value ) , null ) ;
369398 }
370- else if ( objProperty . PropertyType . Equals ( typeof ( bool ) ) && ! objFields [ objFieldMapping . FieldName ] . Value . GetType ( ) . Equals ( typeof ( bool ) ) )
399+ else if (
400+ ( objProperty . PropertyType . Equals ( typeof ( bool ) ) || ( nullableBaseType != null && nullableBaseType . Equals ( typeof ( bool ) ) ) ) &&
401+ ! objFields [ objFieldMapping . FieldName ] . Value . GetType ( ) . Equals ( typeof ( bool ) ) )
371402 {
372- //MySQL connection provides BIT data as a ULong data type, so convert to boolean.
373- objProperty . SetValue ( objObject , System . Convert . ToInt32 ( objFields [ objFieldMapping . FieldName ] . Value ) != 0 , null ) ;
403+ var value = objFields [ objFieldMapping . FieldName ] . Value ;
404+
405+ if ( value != null )
406+ value = System . Convert . ToInt32 ( value ) != 0 ;
407+
408+ // MySQL connection provides BIT data as a ULong data type, so convert to boolean.
409+ objProperty . SetValue ( objObject , value , null ) ;
374410 }
375411 else
376412 {
377- objProperty . SetValue ( objObject , objFields [ objFieldMapping . FieldName ] . Value , null ) ;
413+ if ( nullableBaseType != null )
414+ objProperty . SetValue ( objObject , Convert . ChangeType ( objFields [ objFieldMapping . FieldName ] . Value , nullableBaseType ) , null ) ;
415+ else
416+ objProperty . SetValue ( objObject , objFields [ objFieldMapping . FieldName ] . Value , null ) ;
378417 }
379418 }
380419 catch ( Exception ex )
@@ -387,5 +426,13 @@ private static void LoadFieldsForObject(object objObject, System.Type objType, S
387426 }
388427 }
389428 }
429+
430+ private static Type GetNullableBaseType ( Type type )
431+ {
432+ if ( type . IsGenericType && type . GetGenericTypeDefinition ( ) . Equals ( typeof ( Nullable < > ) ) )
433+ return type . GetGenericArguments ( ) [ 0 ] ;
434+ else
435+ return null ;
436+ }
390437 }
391438}
0 commit comments