     ''' Arrays: arrays of simple types or complex types.
        ''' Key value pairs: <see cref="KeyValuePair(Of TKey,TValue)"/>
        ''' Tuples: <see cref="Tuple(Of T1)"/>, <see cref="Tuple(Of T1,T2)"/>, etc
        ''' Dictionaries: <see cref="IDictionary(Of TKey,TValue)"/> or anything deriving from <see cref="IDictionary(Of TKey,TValue)"/>.
        ''' Collections: <see cref="IList(Of T)"/>, <see cref="IEnumerable(Of T)"/>, <see cref="ICollection(Of T)"/>, <see cref="IList"/>, <see cref="IEnumerable"/>, <see cref="ICollection"/> or anything deriving from <see cref="ICollection(Of T)"/> or <see cref="IList"/>.
        ''' Queryables: <see cref="System.Linq.IQueryable"/>, <see cref="IQueryable(Of T)"/>.
        ''' </summary>
        ''' <param name="type">The type.</param>
        ''' <returns>An object of the given type.</returns>
        Public Function GenerateObject(type As Type) As Object
            GenerateObject = GenerateObject(type, New Dictionary(Of Type, Object)())
        End Function

        <SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification:="Here we just want to return null if anything goes wrong.")>
        Private Function GenerateObject(type As Type, createdObjectReferences As Dictionary(Of Type, Object)) As Object
            Try
                If (SimpleTypeObjectGenerator.CanGenerateObject(type)) Then
                    Return SimpleObjectGenerator.GenerateObject(type)
                End If

                If (type.IsArray) Then
                    Return GenerateArray(type, DefaultCollectionSize, createdObjectReferences)
                End If

                If (type.IsGenericType) Then
                    Return GenerateGenericType(type, DefaultCollectionSize, createdObjectReferences)
                End If

                If (type Is GetType(IDictionary)) Then
                    Return GenerateDictionary(GetType(Hashtable), DefaultCollectionSize, createdObjectReferences)
                End If

                If (GetType(IDictionary).IsAssignableFrom(type)) Then
                    Return GenerateDictionary(type, DefaultCollectionSize, createdObjectReferences)
                End If

                If (type Is GetType(IList) Or
                        type Is GetType(IEnumerable) Or
                        type Is GetType(ICollection)) Then
                    Return GenerateCollection(GetType(ArrayList), DefaultCollectionSize, createdObjectReferences)
                End If

                If (GetType(IList).IsAssignableFrom(type)) Then
                    Return GenerateCollection(type, DefaultCollectionSize, createdObjectReferences)
                End If

                If (type Is GetType(IQueryable)) Then
                    Return GenerateQueryable(type, DefaultCollectionSize, createdObjectReferences)
                End If

                If (type.IsEnum) Then
                    Return GenerateEnum(type)
                End If

                If (type.IsPublic Or type.IsNestedPublic) Then
                    Return GenerateComplexObject(type, createdObjectReferences)
                End If
            Catch
                ' Returns Nothing if anything fails
                Return Nothing
            End Try

            Return Nothing
        End Function

        Private Shared Function GenerateGenericType(type As Type, collectionSize As Integer, createdObjectReferences As Dictionary(Of Type, Object)) As Object
            Dim genericTypeDefinition As Type = type.GetGenericTypeDefinition()

            If (genericTypeDefinition Is GetType(Nullable()) Or genericTypeDefinition Is GetType(Nullable(Of ))) Then
                Return GenerateNullable(type, createdObjectReferences)
            End If

            If (genericTypeDefinition Is GetType(KeyValuePair(Of ,))) Then
                Return GenerateKeyValuePair(type, createdObjectReferences)
            End If

            If (IsTuple(genericTypeDefinition)) Then
                Return GenerateTuple(type, createdObjectReferences)
            End If

            Dim genericArguments() As Type = type.GetGenericArguments()

            If (genericArguments.Length = 1) Then
                If (genericTypeDefinition Is GetType(IList(Of )) Or
                    genericTypeDefinition Is GetType(IEnumerable(Of )) Or
                    genericTypeDefinition Is GetType(ICollection(Of ))) Then

                    Dim collectionType As Type = GetType(List(Of )).MakeGenericType(genericArguments)
                    Return GenerateCollection(collectionType, collectionSize, createdObjectReferences)
                End If

                If (genericTypeDefinition Is GetType(IQueryable(Of ))) Then
                    Return GenerateQueryable(type, collectionSize, createdObjectReferences)
                End If

                Dim closedCollectionType As Type = GetType(ICollection(Of )).MakeGenericType(genericArguments(0))
                If (closedCollectionType.IsAssignableFrom(type)) Then
                    Return GenerateCollection(type, collectionSize, createdObjectReferences)
                End If
            End If

            If (genericArguments.Length = 2) Then
                If (genericTypeDefinition Is GetType(IDictionary(Of ,))) Then
                    Dim dictionaryType As Type = GetType(Dictionary(Of ,)).MakeGenericType(genericArguments)
                    Return GenerateDictionary(dictionaryType, collectionSize, createdObjectReferences)
                End If

                Dim closedDictionaryType As Type = GetType(IDictionary(Of ,)).MakeGenericType(genericArguments(0), genericArguments(1))
                If (closedDictionaryType.IsAssignableFrom(type)) Then
                    Return GenerateDictionary(type, collectionSize, createdObjectReferences)
                End If
            End If

            If (type.IsPublic Or type.IsNestedPublic) Then
                Return GenerateComplexObject(type, createdObjectReferences)
            End If

            Return Nothing
        End Function

        Private Shared Function GenerateTuple(type As Type, createdObjectReferences As Dictionary(Of Type, Object)) As Object
            Dim genericArgs() As Type = type.GetGenericArguments()
            Dim parameterValues(genericArgs.Length - 1) As Object
            Dim failedToCreateTuple As Boolean = True

            Dim objectGenerator As New ObjectGenerator()
            For i As Integer = 0 To genericArgs.Length - 1
                parameterValues(i) = objectGenerator.GenerateObject(genericArgs(i), createdObjectReferences)
                failedToCreateTuple = failedToCreateTuple And (parameterValues(i) Is Nothing)
            Next

            If (failedToCreateTuple) Then
                Return Nothing
            End If

            Return Activator.CreateInstance(type, parameterValues)
        End Function

        Private Shared Function IsTuple(genericTypeDefinition As Type) As Boolean
            Return (genericTypeDefinition Is Ge