private static void PrintProperties(object myObject, int level) { foreach (PropertyInfo property in myObject.GetType().GetProperties()) { try { if (property.CanRead && property.GetGetMethod().GetParameters().Length == 0) { object propertyValue = property.GetValue(myObject); Console.WriteLine(new string(' ', level * 3) + "[Property] {0} {1}: {2}", property.PropertyType.Name, property.Name, propertyValue == null ? "null" : propertyValue); } if (!_valueTypes.Has(property.PropertyType)) { if (property.GetValue(myObject) != null && property.GetValue(myObject).GetType() != myObject.GetType()) { PrintObjectInfo(property.GetValue(myObject), ++level); } } } catch (Exception) { } } }
public object Generate <T>() { T enumObj; if (!typeof(T).IsInterface && !typeof(T).IsAbstract) { enumObj = (T)Activator.CreateInstance(typeof(T)); Type genericTypeArgument = typeof(T).GetGenericArguments().First(); for (int i = 0; i < 10; ++i) { object argument = new object(); string typeName; if (_supportedTypes.Has(genericTypeArgument, out typeName)) { switch (typeName) { case "Byte": IRandomValueGenerator byteGenerator = new RandomByteGenerator(); argument = byteGenerator.Generate <T>(); break; case "Double": IRandomValueGenerator doubleGenerator = new RandomDoubleGenerator(); argument = doubleGenerator.Generate <T>(); break; case "Boolean": IRandomValueGenerator boolGenerator = new RandomBoolGenerator(); argument = boolGenerator.Generate <T>(); break; case "String": IRandomValueGenerator stringGenerator = new RandomStringGenerator(); argument = stringGenerator.Generate <T>(); break; case "DateTime": IRandomValueGenerator dateTimeGenerator = new RandomDateTimeGenerator.RandomDateTimeGenerator(); argument = dateTimeGenerator.Generate <T>(); break; } MethodInfo method = typeof(T).GetMethod("Add"); method.Invoke(enumObj, new object[] { argument }); } else { throw new Exception(); } } return(enumObj); } return(null); }