Пример #1
0
        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;
        }
Пример #2
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
                var objectGenerator = new ObjectGenerator();
                sampleObject = objectGenerator.GenerateObject(type);
            }

            return sampleObject;
        }
Пример #3
0
        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;
        }
Пример #4
0
 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);
         }
     }
 }
Пример #5
0
 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);
     }
 }
Пример #6
0
 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;
 }
Пример #7
0
 private static object GenerateNullable(Type nullableType, Dictionary<Type, object> createdObjectReferences)
 {
     Type 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)
 {
     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;
 }
Пример #9
0
        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;
        }