ThreePointZero.Areas.HelpPage.ObjectGenerator.GenerateObject C# (CSharp) Method

GenerateObject() public method

Generates an object for a given type. The type needs to be public, have a public default constructor and settable public properties/fields. Currently it supports the following types: Simple types: int, string, Enum, DateTime, Uri, etc. Complex types: POCO types. Nullables: Nullable{T}. Arrays: arrays of simple types or complex types. Key value pairs: KeyValuePair{TKey,TValue} Tuples: Tuple{T1}, Tuple{T1,T2}, etc Dictionaries: IDictionary{TKey,TValue} or anything deriving from IDictionary{TKey,TValue}. Collections: IList{T}, IEnumerable{T}, ICollection{T}, IList, IEnumerable, ICollection or anything deriving from ICollection{T} or IList. Queryables: IQueryable, IQueryable{T}.
public GenerateObject ( Type type ) : object
type System.Type The type.
return object
        public object GenerateObject(Type type)
        {
            return GenerateObject(type, new Dictionary<Type, object>());
        }

Same methods

ObjectGenerator::GenerateObject ( Type type, object>.Dictionary createdObjectReferences ) : object

Usage Example

        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;
            ObjectGenerator 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;
        }
All Usage Examples Of ThreePointZero.Areas.HelpPage.ObjectGenerator::GenerateObject