@@ -300,7 +300,7 @@ private static bool TypeIsClrType(Type type)
300
300
/// </returns>
301
301
private static bool TypeIsValidForObjectFiller ( Type type , FillerSetupItem currentSetupItem )
302
302
{
303
- var result = HasTypeARandomFunc ( type , currentSetupItem )
303
+ var result = HasTypeARandomFunc ( type , currentSetupItem )
304
304
|| ( TypeIsList ( type ) && ListParamTypeIsValid ( type , currentSetupItem ) )
305
305
|| ( TypeIsDictionary ( type ) && DictionaryParamTypesAreValid ( type , currentSetupItem ) )
306
306
|| TypeIsPoco ( type )
@@ -342,7 +342,7 @@ private bool CheckForCircularReference(
342
342
{
343
343
throw new InvalidOperationException (
344
344
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." ,
346
346
targetType . Name ) ) ;
347
347
}
348
348
@@ -413,7 +413,7 @@ private object CreateAndFillObject(
413
413
return this . CreateInstanceOfInterfaceOrAbstractClass ( type , currentSetupItem , typeTracker ) ;
414
414
}
415
415
416
- if ( TypeIsEnum ( type ) )
416
+ if ( TypeIsEnum ( type ) || TypeIsNullableEnum ( type ) )
417
417
{
418
418
return this . GetRandomEnumValue ( type ) ;
419
419
}
@@ -467,7 +467,7 @@ private object CreateInstanceOfInterfaceOrAbstractClass(
467
467
{
468
468
string message =
469
469
string . Format (
470
- "ObjectFiller Interface mocker missing and dictionaryType [{0}] not registered" ,
470
+ "ObjectFiller Interface mocker missing and type [{0}] not registered" ,
471
471
interfaceType . Name ) ;
472
472
throw new InvalidOperationException ( message ) ;
473
473
}
@@ -525,7 +525,7 @@ private object CreateInstanceOfType(Type type, FillerSetupItem currentSetupItem,
525
525
526
526
if ( constructorArgs . Count == 0 )
527
527
{
528
- var message = "Could not found a constructor for dictionaryType [" + type . Name
528
+ var message = "Could not found a constructor for type [" + type . Name
529
529
+ "] where the parameters can be filled with the current objectfiller setup" ;
530
530
throw new InvalidOperationException ( message ) ;
531
531
}
@@ -779,7 +779,10 @@ private IEnumerable<PropertyInfo> GetPropertyFromProperties(
779
779
private object GetRandomEnumValue ( Type type )
780
780
{
781
781
// 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 ) ;
783
786
if ( values . Length > 0 )
784
787
{
785
788
int index = Random . Next ( ) % values . Length ;
@@ -816,7 +819,7 @@ private object GetRandomValue(Type propertyType, FillerSetupItem setupItem)
816
819
return GetDefaultValueOfType ( propertyType ) ;
817
820
}
818
821
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." ;
820
823
throw new TypeInitializationException ( propertyType . FullName , new Exception ( message ) ) ;
821
824
}
822
825
@@ -930,6 +933,17 @@ private static bool TypeIsEnum(Type type)
930
933
return type . IsEnum ;
931
934
}
932
935
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
+
933
947
#endregion
934
948
}
935
949
}
0 commit comments