Skip to content

Instantly share code, notes, and snippets.

@Anduin2017
Created July 2, 2019 15:07
Show Gist options
  • Save Anduin2017/ada410ca893b1b8619270538108d1e71 to your computer and use it in GitHub Desktop.
Save Anduin2017/ada410ca893b1b8619270538108d1e71 to your computer and use it in GitHub Desktop.
Generate object
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace Generator
{
public abstract class Conversation
{
}
public class PConversation : Conversation
{
public string MYCName { get; set; }
}
public class GConversation : Conversation
{
public string[] MGName { get; set; }
}
public enum Sex
{
male,
female
}
public class Son
{
public string Name { get; set; }
public Sex Sex { get; set; }
}
public class MyClass
{
public int Count { get; set; }
public string Message { get; set; }
public List<string> Teachers1 { get; set; }
public string[] Teachers2 { get; set; }
public List<Son> Sons { get; set; }
public Son[] Sons2 { get; set; }
public Son MySon { get; set; }
public bool IsMale { get; set; }
public Conversation[] Conversations { get; set; }
public List<Conversation> Conversations2 { get; set; }
}
public static class InstanceGenerator
{
public static IList GetArrayWithInstanceInherts(Type itemType)
{
var listType = typeof(List<>);
var constructedListType = listType.MakeGenericType(itemType);
var instance = (IList)Activator.CreateInstance(constructedListType);
if (!itemType.IsAbstract)
{
instance.Add(Generate(itemType));
}
foreach (var item in Assembly.GetEntryAssembly().GetTypes().Where(t => !t.IsAbstract).Where(t => t.IsSubclassOf(itemType)))
{
instance.Add(Generate(item));
}
return instance;
}
public static object Generate(Type type)
{
if (type == typeof(string))
{
return "an example string.";
}
else if (type == typeof(int))
{
return 0;
}
else if (type == typeof(bool))
{
return true;
}
// List
else if (type.IsGenericType && type.GetGenericTypeDefinition().GetInterfaces().Any(t => t.IsAssignableFrom(typeof(IEnumerable))))
{
Type itemType = type.GetGenericArguments()[0];
return GetArrayWithInstanceInherts(itemType);
}
// Array
else if (type.GetInterface(typeof(IEnumerable<>).FullName) != null)
{
Type itemType = type.GetElementType();
var list = GetArrayWithInstanceInherts(itemType);
Array array = Array.CreateInstance(itemType, list.Count);
list.CopyTo(array, 0);
return array;
}
else
{
var instance = Assembly.GetAssembly(type).CreateInstance(type.FullName);
foreach (var property in instance.GetType().GetProperties())
{
property.SetValue(instance, Generate(property.PropertyType));
}
return instance;
}
}
}
class Program
{
static void Main(string[] args)
{
var instance = InstanceGenerator.Generate(typeof(MyClass));
var result = JsonConvert.SerializeObject(instance, Formatting.Indented);
var dobject = JsonConvert.DeserializeObject(result);
var finalresult = JsonConvert.SerializeObject(dobject, Formatting.Indented);
Console.WriteLine(finalresult);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment