@@ -300,7 +300,7 @@ private static bool TypeIsClrType(Type type)
300300 /// </returns>
301301 private static bool TypeIsValidForObjectFiller ( Type type , FillerSetupItem currentSetupItem )
302302 {
303- var result = HasTypeARandomFunc ( type , currentSetupItem )
303+ var result = HasTypeARandomFunc ( type , currentSetupItem )
304304 || ( TypeIsList ( type ) && ListParamTypeIsValid ( type , currentSetupItem ) )
305305 || ( TypeIsDictionary ( type ) && DictionaryParamTypesAreValid ( type , currentSetupItem ) )
306306 || TypeIsPoco ( type )
@@ -342,7 +342,7 @@ private bool CheckForCircularReference(
342342 {
343343 throw new InvalidOperationException (
344344 string . Format (
345- "The dictionaryType {0} was already encountered before, which probably means you have a circular reference in your model. Either ignore the properties which cause this or specify explicit creation rules for them which do not rely on types." ,
345+ "The type {0} was already encountered before, which probably means you have a circular reference in your model. Either ignore the properties which cause this or specify explicit creation rules for them which do not rely on types." ,
346346 targetType . Name ) ) ;
347347 }
348348
@@ -413,7 +413,7 @@ private object CreateAndFillObject(
413413 return this . CreateInstanceOfInterfaceOrAbstractClass ( type , currentSetupItem , typeTracker ) ;
414414 }
415415
416- if ( TypeIsEnum ( type ) )
416+ if ( TypeIsEnum ( type ) || TypeIsNullableEnum ( type ) )
417417 {
418418 return this . GetRandomEnumValue ( type ) ;
419419 }
@@ -467,7 +467,7 @@ private object CreateInstanceOfInterfaceOrAbstractClass(
467467 {
468468 string message =
469469 string . Format (
470- "ObjectFiller Interface mocker missing and dictionaryType [{0}] not registered" ,
470+ "ObjectFiller Interface mocker missing and type [{0}] not registered" ,
471471 interfaceType . Name ) ;
472472 throw new InvalidOperationException ( message ) ;
473473 }
@@ -525,7 +525,7 @@ private object CreateInstanceOfType(Type type, FillerSetupItem currentSetupItem,
525525
526526 if ( constructorArgs . Count == 0 )
527527 {
528- var message = "Could not found a constructor for dictionaryType [" + type . Name
528+ var message = "Could not found a constructor for type [" + type . Name
529529 + "] where the parameters can be filled with the current objectfiller setup" ;
530530 throw new InvalidOperationException ( message ) ;
531531 }
@@ -779,7 +779,10 @@ private IEnumerable<PropertyInfo> GetPropertyFromProperties(
779779 private object GetRandomEnumValue ( Type type )
780780 {
781781 // performance: Enum.GetValues() is slow due to reflection, should cache it
782- Array values = Enum . GetValues ( type ) ;
782+
783+ var enumType = type . IsEnum ? type : Nullable . GetUnderlyingType ( type ) ;
784+
785+ Array values = Enum . GetValues ( enumType ) ;
783786 if ( values . Length > 0 )
784787 {
785788 int index = Random . Next ( ) % values . Length ;
@@ -816,7 +819,7 @@ private object GetRandomValue(Type propertyType, FillerSetupItem setupItem)
816819 return GetDefaultValueOfType ( propertyType ) ;
817820 }
818821
819- string message = "The dictionaryType [" + propertyType . Name + "] was not registered in the randomizer." ;
822+ string message = "The type [" + propertyType . Name + "] was not registered in the randomizer." ;
820823 throw new TypeInitializationException ( propertyType . FullName , new Exception ( message ) ) ;
821824 }
822825
@@ -930,6 +933,17 @@ private static bool TypeIsEnum(Type type)
930933 return type . IsEnum ;
931934 }
932935
936+ /// <summary>
937+ /// Checks if the given <see cref="type"/> is a nullable enum
938+ /// </summary>
939+ /// <param name="type">Type to check</param>
940+ /// <returns>True if the type is a nullable enum</returns>
941+ private static bool TypeIsNullableEnum ( Type type )
942+ {
943+ Type u = Nullable . GetUnderlyingType ( type ) ;
944+ return ( u != null ) && u . IsEnum ;
945+ }
946+
933947 #endregion
934948 }
935949}
0 commit comments