@@ -13,6 +13,7 @@ namespace Tynamix.ObjectFiller
1313 using System ;
1414 using System . Collections ;
1515 using System . Collections . Generic ;
16+ using System . Collections . ObjectModel ;
1617 using System . Linq ;
1718 using System . Reflection ;
1819
@@ -254,9 +255,29 @@ private static bool TypeIsDictionary(Type type)
254255 /// </returns>
255256 private static bool TypeIsList ( Type type )
256257 {
257- return ! type . IsArray && type . IsGenericType ( ) && type . GetGenericTypeArguments ( ) . Length != 0
258+ return ! type . IsArray
259+ && type . IsGenericType ( )
260+ && type . GetGenericTypeArguments ( ) . Length != 0
258261 && ( type . GetGenericTypeDefinition ( ) == typeof ( IEnumerable < > )
259- || type . GetImplementedInterfaces ( ) . Any ( x => x == typeof ( IEnumerable ) ) ) ;
262+ || type . GetImplementedInterfaces ( ) . Any ( x => x == typeof ( IList ) ) ) ;
263+ }
264+
265+ /// <summary>
266+ /// Checks if the given <see cref="type"/> is a list
267+ /// </summary>
268+ /// <param name="type">
269+ /// The type to check
270+ /// </param>
271+ /// <returns>
272+ /// True if the target <see cref="type"/> is a list
273+ /// </returns>
274+ private static bool TypeIsCollection ( Type type )
275+ {
276+ return ! type . IsArray
277+ && type . IsGenericType ( )
278+ && type . GetGenericTypeArguments ( ) . Length != 0
279+ && ( type . GetGenericTypeDefinition ( ) == typeof ( ICollection < > )
280+ || type . GetImplementedInterfaces ( ) . Any ( x => x . GetGenericTypeDefinition ( ) == typeof ( ICollection < > ) ) ) ;
260281 }
261282
262283 /// <summary>
@@ -409,6 +430,13 @@ private object CreateAndFillObject(
409430 return list ;
410431 }
411432
433+ if ( TypeIsCollection ( type ) )
434+ {
435+ IEnumerable collection = this . GetFilledCollection ( type , currentSetupItem , typeTracker ) ;
436+
437+ return collection ;
438+ }
439+
412440 if ( TypeIsArray ( type ) )
413441 {
414442 Array array = this . GetFilledArray ( type , currentSetupItem , typeTracker ) ;
@@ -720,7 +748,7 @@ private IList GetFilledList(Type propertyType, FillerSetupItem currentSetupItem,
720748
721749 IList list ;
722750 if ( ! propertyType . IsInterface ( )
723- && propertyType . GetImplementedInterfaces ( ) . Any ( x => x . GetGenericTypeDefinition ( ) == typeof ( ICollection < > ) ) )
751+ && propertyType . GetImplementedInterfaces ( ) . Any ( x => x == typeof ( IList ) ) )
724752 {
725753 list = ( IList ) Activator . CreateInstance ( propertyType ) ;
726754 }
@@ -746,6 +774,60 @@ private IList GetFilledList(Type propertyType, FillerSetupItem currentSetupItem,
746774 return list ;
747775 }
748776
777+ /// <summary>
778+ /// Creates and fills a list of the given <see cref="propertyType"/>
779+ /// </summary>
780+ /// <param name="propertyType">
781+ /// Type of the list
782+ /// </param>
783+ /// <param name="currentSetupItem">
784+ /// The current setup item.
785+ /// </param>
786+ /// <param name="typeTracker">
787+ /// The dictionaryType tracker to find circular dependencies
788+ /// </param>
789+ /// <returns>
790+ /// Created and filled list of the given <see cref="propertyType"/>
791+ /// </returns>
792+ private IEnumerable GetFilledCollection ( Type propertyType , FillerSetupItem currentSetupItem , HashStack < Type > typeTracker )
793+ {
794+ Type genType = propertyType . GetGenericTypeArguments ( ) [ 0 ] ;
795+
796+ if ( this . CheckForCircularReference ( genType , typeTracker , currentSetupItem ) )
797+ {
798+ return null ;
799+ }
800+
801+ IEnumerable target ;
802+
803+ if ( ! propertyType . IsInterface ( )
804+ && propertyType . GetImplementedInterfaces ( ) . Any ( x => x . GetGenericTypeDefinition ( ) == typeof ( ICollection < > ) ) )
805+ {
806+ target = ( IEnumerable ) Activator . CreateInstance ( propertyType ) ;
807+ }
808+ else if ( propertyType . IsGenericType ( ) && propertyType . GetGenericTypeDefinition ( ) == typeof ( ICollection < > )
809+ || propertyType . GetImplementedInterfaces ( ) . Any ( x => x . GetGenericTypeDefinition ( ) == typeof ( ICollection < > ) ) )
810+ {
811+ Type openListType = typeof ( List < > ) ;
812+ Type genericListType = openListType . MakeGenericType ( genType ) ;
813+ target = ( IEnumerable ) Activator . CreateInstance ( genericListType ) ;
814+ }
815+ else
816+ {
817+ target = ( IEnumerable ) Activator . CreateInstance ( propertyType ) ;
818+ }
819+
820+ int maxListItems = Random . Next ( currentSetupItem . ListMinCount , currentSetupItem . ListMaxCount ) ;
821+ for ( int i = 0 ; i < maxListItems ; i ++ )
822+ {
823+ object listObject = this . CreateAndFillObject ( genType , currentSetupItem , typeTracker ) ;
824+ MethodInfo method = target . GetType ( ) . GetMethod ( "Add" ) ;
825+ method . Invoke ( target , new object [ ] { listObject } ) ;
826+ }
827+
828+ return target ;
829+ }
830+
749831 /// <summary>
750832 /// Creates and fills a POCO class
751833 /// </summary>
0 commit comments