GenerateObject() public method

Generates an object for a given type. The type needs to be public, have a public default constructor and settable public properties/fields. Currently it supports the following types: Simple types: int, string, Enum, DateTime, Uri, etc. Complex types: POCO types. Nullables: Nullable{T}. Arrays: arrays of simple types or complex types. Key value pairs: KeyValuePair{TKey,TValue} Tuples: Tuple{T1}, Tuple{T1,T2}, etc Dictionaries: IDictionary{TKey,TValue} or anything deriving from IDictionary{TKey,TValue}. Collections: IList{T}, IEnumerable{T}, ICollection{T}, IList, IEnumerable, ICollection or anything deriving from ICollection{T} or IList. Queryables: IQueryable, IQueryable{T}.
public GenerateObject ( Type type ) : object
type Type The type.
return object
        private static object GenerateArray(Type arrayType, int size, Dictionary<Type, object> createdObjectReferences)
        {
            Type type = arrayType.GetElementType();
            Array result = Array.CreateInstance(type, size);
            bool areAllElementsNull = true;
            ObjectGenerator objectGenerator = new ObjectGenerator();
            for (int i = 0; i < size; i++)
            {
                object element = objectGenerator.GenerateObject(type, createdObjectReferences);
                result.SetValue(element, i);
                areAllElementsNull &= element == null;
            }

            if (areAllElementsNull)
            {
                return null;
            }

            return result;
        }
        private static object GenerateCollection(Type collectionType, int size, Dictionary<Type, object> createdObjectReferences)
        {
            Type type = collectionType.IsGenericType ?
                collectionType.GetGenericArguments()[0] :
                typeof(object);
            object result = Activator.CreateInstance(collectionType);
            MethodInfo addMethod = collectionType.GetMethod("Add");
            bool areAllElementsNull = true;
            ObjectGenerator objectGenerator = new ObjectGenerator();
            for (int i = 0; i < size; i++)
            {
                object element = objectGenerator.GenerateObject(type, createdObjectReferences);
                addMethod.Invoke(result, new object[] { element });
                areAllElementsNull &= element == null;
            }

            if (areAllElementsNull)
            {
                return null;
            }

            return result;
        }
 private static void SetPublicFields(Type type, object obj, Dictionary<Type, object> createdObjectReferences)
 {
     FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance);
     ObjectGenerator objectGenerator = new ObjectGenerator();
     foreach (FieldInfo field in fields)
     {
         object fieldValue = objectGenerator.GenerateObject(field.FieldType, createdObjectReferences);
         field.SetValue(obj, fieldValue);
     }
 }
 private static void SetPublicProperties(Type type, object obj, Dictionary<Type, object> createdObjectReferences)
 {
     PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
     ObjectGenerator objectGenerator = new ObjectGenerator();
     foreach (PropertyInfo property in properties)
     {
         if (property.CanWrite)
         {
             object propertyValue = objectGenerator.GenerateObject(property.PropertyType, createdObjectReferences);
             property.SetValue(obj, propertyValue, null);
         }
     }
 }
 private static object GenerateTuple(Type type, Dictionary<Type, object> createdObjectReferences)
 {
     Type[] genericArgs = type.GetGenericArguments();
     object[] parameterValues = new object[genericArgs.Length];
     bool failedToCreateTuple = true;
     ObjectGenerator objectGenerator = new ObjectGenerator();
     for (int i = 0; i < genericArgs.Length; i++)
     {
         parameterValues[i] = objectGenerator.GenerateObject(genericArgs[i], createdObjectReferences);
         failedToCreateTuple &= parameterValues[i] == null;
     }
     if (failedToCreateTuple)
     {
         return null;
     }
     object result = Activator.CreateInstance(type, parameterValues);
     return result;
 }
 private static object GenerateNullable(Type nullableType, Dictionary<Type, object> createdObjectReferences)
 {
     Type type = nullableType.GetGenericArguments()[0];
     ObjectGenerator objectGenerator = new ObjectGenerator();
     return objectGenerator.GenerateObject(type, createdObjectReferences);
 }
 private static object GenerateKeyValuePair(Type keyValuePairType, Dictionary<Type, object> createdObjectReferences)
 {
     Type[] genericArgs = keyValuePairType.GetGenericArguments();
     Type typeK = genericArgs[0];
     Type typeV = genericArgs[1];
     ObjectGenerator objectGenerator = new ObjectGenerator();
     object keyObject = objectGenerator.GenerateObject(typeK, createdObjectReferences);
     object valueObject = objectGenerator.GenerateObject(typeV, createdObjectReferences);
     if (keyObject == null && valueObject == null)
     {
         // Failed to create key and values
         return null;
     }
     object result = Activator.CreateInstance(keyValuePairType, keyObject, valueObject);
     return result;
 }
        private static object GenerateDictionary(Type dictionaryType, int size, Dictionary<Type, object> createdObjectReferences)
        {
            Type typeK = typeof(object);
            Type typeV = typeof(object);
            if (dictionaryType.IsGenericType)
            {
                Type[] genericArgs = dictionaryType.GetGenericArguments();
                typeK = genericArgs[0];
                typeV = genericArgs[1];
            }

            object result = Activator.CreateInstance(dictionaryType);
            MethodInfo addMethod = dictionaryType.GetMethod("Add") ?? dictionaryType.GetMethod("TryAdd");
            MethodInfo containsMethod = dictionaryType.GetMethod("Contains") ?? dictionaryType.GetMethod("ContainsKey");
            ObjectGenerator objectGenerator = new ObjectGenerator();
            for (int i = 0; i < size; i++)
            {
                object newKey = objectGenerator.GenerateObject(typeK, createdObjectReferences);
                if (newKey == null)
                {
                    // Cannot generate a valid key
                    return null;
                }

                bool containsKey = (bool)containsMethod.Invoke(result, new object[] { newKey });
                if (!containsKey)
                {
                    object newValue = objectGenerator.GenerateObject(typeV, createdObjectReferences);
                    addMethod.Invoke(result, new object[] { newKey, newValue });
                }
            }

            return result;
        }
 // Default factory for sample objects
 private static object DefaultSampleObjectFactory(HelpPageSampleGenerator sampleGenerator, Type type)
 {
     // Try to create a default sample object
     ObjectGenerator objectGenerator = new ObjectGenerator();
     return objectGenerator.GenerateObject(type);
 }