private static void SetPublicProperties(Type type, object obj, Dictionary<Type, object> createdObjectReferences) { var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); var objectGenerator = new ObjectGenerator(); foreach (var property in properties) { if (property.CanWrite) { var propertyValue = objectGenerator.GenerateObject(property.PropertyType, createdObjectReferences); property.SetValue(obj, propertyValue, null); } } }
private static void SetPublicFields(Type type, object obj, Dictionary<Type, object> createdObjectReferences) { var fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance); var objectGenerator = new ObjectGenerator(); foreach (var field in fields) { var fieldValue = objectGenerator.GenerateObject(field.FieldType, createdObjectReferences); field.SetValue(obj, fieldValue); } }
private static object GenerateCollection(Type collectionType, int size, Dictionary<Type, object> createdObjectReferences) { var type = collectionType.IsGenericType ? collectionType.GetGenericArguments()[0] : typeof(object); var result = Activator.CreateInstance(collectionType); var addMethod = collectionType.GetMethod("Add"); var areAllElementsNull = true; var objectGenerator = new ObjectGenerator(); for (var i = 0; i < size; i++) { var element = objectGenerator.GenerateObject(type, createdObjectReferences); addMethod.Invoke(result, new object[] { element }); areAllElementsNull &= element == null; } if (areAllElementsNull) { return null; } return result; }
private static object GenerateNullable(Type nullableType, Dictionary<Type, object> createdObjectReferences) { var type = nullableType.GetGenericArguments()[0]; var objectGenerator = new ObjectGenerator(); return objectGenerator.GenerateObject(type, createdObjectReferences); }
private static object GenerateDictionary(Type dictionaryType, int size, Dictionary<Type, object> createdObjectReferences) { var typeK = typeof(object); var typeV = typeof(object); if (dictionaryType.IsGenericType) { var genericArgs = dictionaryType.GetGenericArguments(); typeK = genericArgs[0]; typeV = genericArgs[1]; } var result = Activator.CreateInstance(dictionaryType); var addMethod = dictionaryType.GetMethod("Add") ?? dictionaryType.GetMethod("TryAdd"); var containsMethod = dictionaryType.GetMethod("Contains") ?? dictionaryType.GetMethod("ContainsKey"); var objectGenerator = new ObjectGenerator(); for (var i = 0; i < size; i++) { var newKey = objectGenerator.GenerateObject(typeK, createdObjectReferences); if (newKey == null) { // Cannot generate a valid key return null; } var containsKey = (bool)containsMethod.Invoke(result, new object[] { newKey }); if (!containsKey) { var newValue = objectGenerator.GenerateObject(typeV, createdObjectReferences); addMethod.Invoke(result, new object[] { newKey, newValue }); } } return result; }
private static object GenerateArray(Type arrayType, int size, Dictionary<Type, object> createdObjectReferences) { var type = arrayType.GetElementType(); var result = Array.CreateInstance(type, size); var areAllElementsNull = true; var objectGenerator = new ObjectGenerator(); for (var i = 0; i < size; i++) { var element = objectGenerator.GenerateObject(type, createdObjectReferences); result.SetValue(element, i); areAllElementsNull &= element == null; } if (areAllElementsNull) { return null; } return result; }
private static object GenerateKeyValuePair(Type keyValuePairType, Dictionary<Type, object> createdObjectReferences) { var genericArgs = keyValuePairType.GetGenericArguments(); var typeK = genericArgs[0]; var typeV = genericArgs[1]; var objectGenerator = new ObjectGenerator(); var keyObject = objectGenerator.GenerateObject(typeK, createdObjectReferences); var valueObject = objectGenerator.GenerateObject(typeV, createdObjectReferences); if (keyObject == null && valueObject == null) { // Failed to create key and values return null; } var result = Activator.CreateInstance(keyValuePairType, keyObject, valueObject); return result; }
private static object GenerateTuple(Type type, Dictionary<Type, object> createdObjectReferences) { var genericArgs = type.GetGenericArguments(); var parameterValues = new object[genericArgs.Length]; var failedToCreateTuple = true; var objectGenerator = new ObjectGenerator(); for (var i = 0; i < genericArgs.Length; i++) { parameterValues[i] = objectGenerator.GenerateObject(genericArgs[i], createdObjectReferences); failedToCreateTuple &= parameterValues[i] == null; } if (failedToCreateTuple) { return null; } var result = Activator.CreateInstance(type, parameterValues); return result; }
// Default factory for sample objects private static object DefaultSampleObjectFactory(HelpPageSampleGenerator sampleGenerator, Type type) { // Try to create a default sample object var objectGenerator = new ObjectGenerator(); return objectGenerator.GenerateObject(type); }