@@ -63,12 +63,12 @@ public T Create()
6363 /// </summary>
6464 public IEnumerable < T > Create ( int count )
6565 {
66- for ( int n = 0 ; n < count ; n ++ )
67- {
68- T objectToFill = ( T ) CreateInstanceOfType ( typeof ( T ) , SetupManager . GetFor < T > ( ) ) ;
69- Fill ( objectToFill ) ;
70- yield return objectToFill ;
71- }
66+ for ( int n = 0 ; n < count ; n ++ )
67+ {
68+ T objectToFill = ( T ) CreateInstanceOfType ( typeof ( T ) , SetupManager . GetFor < T > ( ) ) ;
69+ Fill ( objectToFill ) ;
70+ yield return objectToFill ;
71+ }
7272 }
7373
7474 /// <summary>
@@ -122,9 +122,9 @@ private object CreateInstanceOfType(Type type, ObjectFillerSetup currentSetup)
122122
123123 if ( constructorArgs . Count == 0 )
124124 {
125- var message = "Could not found a constructor for type [" + type . Name + "] where the parameters can be filled with the current objectfiller setup" ;
126- Debug . WriteLine ( "ObjectFiller: " + message ) ;
127- throw new InvalidOperationException ( message ) ;
125+ var message = "Could not found a constructor for type [" + type . Name + "] where the parameters can be filled with the current objectfiller setup" ;
126+ Debug . WriteLine ( "ObjectFiller: " + message ) ;
127+ throw new InvalidOperationException ( message ) ;
128128 }
129129 }
130130 }
@@ -268,30 +268,30 @@ private object GetFilledObject(Type type, ObjectFillerSetup currentSetup)
268268 return GetFilledPoco ( type , currentSetup ) ;
269269 }
270270
271- if ( TypeIsEnum ( type ) )
272- {
273- return GetRandomEnumValue ( type ) ;
274- }
271+ if ( TypeIsEnum ( type ) )
272+ {
273+ return GetRandomEnumValue ( type ) ;
274+ }
275275
276276 object newValue = GetRandomValue ( type , currentSetup ) ;
277277 return newValue ;
278278 }
279279
280280
281281
282- private object GetRandomEnumValue ( Type type )
283- {
284- // performance: Enum.GetValues() is slow due to reflection, should cache it
285- Array values = Enum . GetValues ( type ) ;
286- if ( values . Length > 0 )
287- {
288- int index = Random . Next ( ) % values . Length ;
289- return values . GetValue ( index ) ;
290- }
291- return 0 ;
292- }
282+ private object GetRandomEnumValue ( Type type )
283+ {
284+ // performance: Enum.GetValues() is slow due to reflection, should cache it
285+ Array values = Enum . GetValues ( type ) ;
286+ if ( values . Length > 0 )
287+ {
288+ int index = Random . Next ( ) % values . Length ;
289+ return values . GetValue ( index ) ;
290+ }
291+ return 0 ;
292+ }
293293
294- private object GetFilledPoco ( Type type , ObjectFillerSetup currentSetup )
294+ private object GetFilledPoco ( Type type , ObjectFillerSetup currentSetup )
295295 {
296296 object result = CreateInstanceOfType ( type , currentSetup ) ;
297297
@@ -313,12 +313,12 @@ private IDictionary GetFilledDictionary(Type propertyType, ObjectFillerSetup cur
313313
314314 if ( dictionary . Contains ( keyObject ) )
315315 {
316- string message = string . Format ( "Generating Keyvalue failed because it generates always the same data for type [{0}]. Please check your setup." , keyType ) ;
317- Debug . WriteLine ( "ObjectFiller: " + message ) ;
318- throw new ArgumentException ( message ) ;
316+ string message = string . Format ( "Generating Keyvalue failed because it generates always the same data for type [{0}]. Please check your setup." , keyType ) ;
317+ Debug . WriteLine ( "ObjectFiller: " + message ) ;
318+ throw new ArgumentException ( message ) ;
319319 }
320320
321- object valueObject = GetFilledObject ( valueType , currentSetup ) ;
321+ object valueObject = GetFilledObject ( valueType , currentSetup ) ;
322322 dictionary . Add ( keyObject , valueObject ) ;
323323 }
324324 return dictionary ;
@@ -378,12 +378,12 @@ private object GetInterfaceInstance(Type interfaceType, ObjectFillerSetup setup)
378378 {
379379 if ( setup . InterfaceMocker == null )
380380 {
381- string message = string . Format ( "ObjectFiller Interface mocker missing and type [{0}] not registered" , interfaceType . Name ) ;
382- Debug . WriteLine ( "ObjectFiller: " + message ) ;
383- throw new InvalidOperationException ( message ) ;
381+ string message = string . Format ( "ObjectFiller Interface mocker missing and type [{0}] not registered" , interfaceType . Name ) ;
382+ Debug . WriteLine ( "ObjectFiller: " + message ) ;
383+ throw new InvalidOperationException ( message ) ;
384384 }
385385
386- MethodInfo method = setup . InterfaceMocker . GetType ( ) . GetMethod ( "Create" ) ;
386+ MethodInfo method = setup . InterfaceMocker . GetType ( ) . GetMethod ( "Create" ) ;
387387 MethodInfo genericMethod = method . MakeGenericMethod ( new [ ] { interfaceType } ) ;
388388 result = genericMethod . Invoke ( setup . InterfaceMocker , null ) ;
389389 }
@@ -398,9 +398,18 @@ private object GetRandomValue(Type propertyType, ObjectFillerSetup setup)
398398 return setup . TypeToRandomFunc [ propertyType ] ( ) ;
399399 }
400400
401- string message = "The type [" + propertyType . Name + "] was not registered in the randomizer." ;
402- Debug . WriteLine ( "ObjectFiller: " + message ) ;
403- throw new TypeInitializationException ( propertyType . FullName , new Exception ( message ) ) ;
401+ if ( setup . IgnoreAllUnknownTypes )
402+ {
403+ if ( propertyType . IsValueType )
404+ {
405+ return Activator . CreateInstance ( propertyType ) ;
406+ }
407+ return null ;
408+ }
409+
410+ string message = "The type [" + propertyType . Name + "] was not registered in the randomizer." ;
411+ Debug . WriteLine ( "ObjectFiller: " + message ) ;
412+ throw new TypeInitializationException ( propertyType . FullName , new Exception ( message ) ) ;
404413 }
405414
406415 private static bool TypeIsValidForObjectFiller ( Type type , ObjectFillerSetup currentSetup )
@@ -465,9 +474,9 @@ private static bool TypeIsList(Type type)
465474 || type . GetInterfaces ( ) . Any ( x => x == typeof ( IEnumerable ) ) ) ;
466475 }
467476
468- private static bool TypeIsEnum ( Type type )
469- {
470- return type . IsEnum ;
471- }
472- }
477+ private static bool TypeIsEnum ( Type type )
478+ {
479+ return type . IsEnum ;
480+ }
481+ }
473482}
0 commit comments