示例#1
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;
        }
示例#2
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 object[] { element });
                areAllElementsNull &= element == null;
            }

            if (areAllElementsNull)
            {
                return null;
            }

            return result;
        }
示例#3
0
        /// <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 (!SampleObjects.TryGetValue(type, out sampleObject))
            {
                // Try create a default sample object
                ObjectGenerator objectGenerator = new ObjectGenerator();
                sampleObject = objectGenerator.GenerateObject(type);
            }

            return sampleObject;
        }
示例#4
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)
         {
             object propertyValue;
             if (!TrySetKnowFieldTypesByName(property.Name, property.PropertyType, type, out propertyValue))
                 propertyValue = objectGenerator.GenerateObject(property.PropertyType, createdObjectReferences);
             property.SetValue(obj, propertyValue, null);
         }
     }
 }
示例#5
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)
     {
         object fieldValue;
         if (!TrySetKnowFieldTypesByName(field.Name, field.FieldType, type, out fieldValue))
             fieldValue = objectGenerator.GenerateObject(field.FieldType, createdObjectReferences);
         field.SetValue(obj, fieldValue);
     }
 }
示例#6
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;
 }
示例#7
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);
 }
示例#8
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;
 }
示例#9
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 object[] { newKey });
                if (!containsKey)
                {
                    var newValue = objectGenerator.GenerateObject(typeV, createdObjectReferences);
                    addMethod.Invoke(result, new object[] { newKey, newValue });
                }
            }

            return result;
        }