Пример #1
0
 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);
         }
     }
 }
Пример #2
0
 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);
     }
 }
Пример #3
0
        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[] {element});
                areAllElementsNull &= element == null;
            }

            if (areAllElementsNull)
            {
                return null;
            }

            return result;
        }
Пример #4
0
 private static object GenerateNullable(Type nullableType, Dictionary<Type, object> createdObjectReferences)
 {
     var type = nullableType.GetGenericArguments()[0];
     var objectGenerator = new ObjectGenerator();
     return objectGenerator.GenerateObject(type, createdObjectReferences);
 }
Пример #5
0
        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[] {newKey});
                if (!containsKey)
                {
                    var newValue = objectGenerator.GenerateObject(typeV, createdObjectReferences);
                    addMethod.Invoke(result, new[] {newKey, newValue});
                }
            }

            return result;
        }
Пример #6
0
        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;
        }
Пример #7
0
 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;
 }
Пример #8
0
 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);
 }