/// <summary> /// The generate array. /// </summary> /// <param name="arrayType"> /// The array type. /// </param> /// <param name="size"> /// The size. /// </param> /// <param name="createdObjectReferences"> /// The created object references. /// </param> /// <returns> /// The <see cref="object"/>. /// </returns> 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; var 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; }
/// <summary> /// Gets the sample object that will be serialized by the formatters. /// First, it will look at the <see cref="SampleObjects"/>. If no sample object is found, it will try to create one /// using <see cref="ObjectGenerator"/>. /// </summary> /// <param name="type"> /// The type. /// </param> /// <returns> /// The sample object. /// </returns> public virtual object GetSampleObject(Type type) { object sampleObject; if (!this.SampleObjects.TryGetValue(type, out sampleObject)) { // Try create a default sample object var objectGenerator = new ObjectGenerator(); sampleObject = objectGenerator.GenerateObject(type); } return sampleObject; }
/// <summary> /// The set public fields. /// </summary> /// <param name="type"> /// The type. /// </param> /// <param name="obj"> /// The obj. /// </param> /// <param name="createdObjectReferences"> /// The created object references. /// </param> private static void SetPublicFields(Type type, object obj, Dictionary<Type, object> createdObjectReferences) { FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance); var objectGenerator = new ObjectGenerator(); foreach (FieldInfo field in fields) { object fieldValue = objectGenerator.GenerateObject(field.FieldType, createdObjectReferences); field.SetValue(obj, fieldValue); } }
/// <summary> /// The set public properties. /// </summary> /// <param name="type"> /// The type. /// </param> /// <param name="obj"> /// The obj. /// </param> /// <param name="createdObjectReferences"> /// The created object references. /// </param> private static void SetPublicProperties(Type type, object obj, Dictionary<Type, object> createdObjectReferences) { PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); var objectGenerator = new ObjectGenerator(); foreach (PropertyInfo property in properties) { if (property.CanWrite) { object propertyValue = objectGenerator.GenerateObject( property.PropertyType, createdObjectReferences); property.SetValue(obj, propertyValue, null); } } }
/// <summary> /// The generate tuple. /// </summary> /// <param name="type"> /// The type. /// </param> /// <param name="createdObjectReferences"> /// The created object references. /// </param> /// <returns> /// The <see cref="object"/>. /// </returns> private static object GenerateTuple(Type type, Dictionary<Type, object> createdObjectReferences) { Type[] genericArgs = type.GetGenericArguments(); var parameterValues = new object[genericArgs.Length]; bool failedToCreateTuple = true; var 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; }
/// <summary> /// The generate nullable. /// </summary> /// <param name="nullableType"> /// The nullable type. /// </param> /// <param name="createdObjectReferences"> /// The created object references. /// </param> /// <returns> /// The <see cref="object"/>. /// </returns> private static object GenerateNullable(Type nullableType, Dictionary<Type, object> createdObjectReferences) { Type type = nullableType.GetGenericArguments()[0]; var objectGenerator = new ObjectGenerator(); return objectGenerator.GenerateObject(type, createdObjectReferences); }
/// <summary> /// The generate key value pair. /// </summary> /// <param name="keyValuePairType"> /// The key value pair type. /// </param> /// <param name="createdObjectReferences"> /// The created object references. /// </param> /// <returns> /// The <see cref="object"/>. /// </returns> private static object GenerateKeyValuePair( Type keyValuePairType, Dictionary<Type, object> createdObjectReferences) { Type[] genericArgs = keyValuePairType.GetGenericArguments(); Type typeK = genericArgs[0]; Type typeV = genericArgs[1]; var 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; }
/// <summary> /// The generate dictionary. /// </summary> /// <param name="dictionaryType"> /// The dictionary type. /// </param> /// <param name="size"> /// The size. /// </param> /// <param name="createdObjectReferences"> /// The created object references. /// </param> /// <returns> /// The <see cref="object"/>. /// </returns> 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"); var 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; } var containsKey = (bool)containsMethod.Invoke(result, new[] { newKey }); if (!containsKey) { object newValue = objectGenerator.GenerateObject(typeV, createdObjectReferences); addMethod.Invoke(result, new[] { newKey, newValue }); } } return result; }
/// <summary> /// The generate collection. /// </summary> /// <param name="collectionType"> /// The collection type. /// </param> /// <param name="size"> /// The size. /// </param> /// <param name="createdObjectReferences"> /// The created object references. /// </param> /// <returns> /// The <see cref="object"/>. /// </returns> 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; var objectGenerator = new ObjectGenerator(); for (int i = 0; i < size; i++) { object element = objectGenerator.GenerateObject(type, createdObjectReferences); addMethod.Invoke(result, new[] { element }); areAllElementsNull &= element == null; } if (areAllElementsNull) { return null; } return result; }