static void Main(string[] args) { char selected = '\0'; while (Char.ToLower(selected) != 'e') { Console.Clear(); Console.WriteLine(""); Console.WriteLine(" ".PadRight(PADRIGHT_VALUE, '_')); Console.WriteLine("|".PadRight(PADRIGHT_VALUE, ' ') + "|"); Console.WriteLine("| 1- For SampleClass".PadRight(PADRIGHT_VALUE, ' ') + "|"); Console.WriteLine("| 2- For SampleAllTypesData".PadRight(PADRIGHT_VALUE, ' ') + "|"); Console.WriteLine("| 3- For ReadClassFromFile(classesconfig.json)".PadRight(PADRIGHT_VALUE, ' ') + "|"); Console.WriteLine("| e- EXIT".PadRight(PADRIGHT_VALUE, ' ') + "|"); Console.WriteLine("|".PadRight(PADRIGHT_VALUE, '_') + "|"); Console.Write("select: "); selected = Console.ReadKey().KeyChar; Console.Clear(); Console.WriteLine(""); Console.WriteLine(" ".PadRight(PADRIGHT_VALUE, '_')); Console.WriteLine("|".PadRight(PADRIGHT_VALUE, ' ') + "|"); switch (Char.ToLower(selected)) { case '1': ConsoleWriteHelper.WritePropertiesonBaseClass(MockDataStatics.SampleClass); break; case '2': ConsoleWriteHelper.WritePropertiesonBaseClass(MockDataStatics.SampleAllTypesData); break; case '3': ClassJsonFile classJsonFile = ClassJsonHelper.ReadClassFromFile(CLASS_CONFIG_FILE); IList objectList = ReflectionHelper.PrepareJsonClasses(classJsonFile); foreach (var obj in objectList) { ConsoleWriteHelper.WritePropertiesonBaseClass(obj); } break; case 'e': return; default: Console.WriteLine("| Wrong key....".PadRight(PADRIGHT_VALUE, ' ') + "|"); break; } Console.WriteLine("|".PadRight(PADRIGHT_VALUE, '_') + "|"); Console.Write("To continue press any key..."); Console.ReadKey(); } }
public static IList PrepareJsonClasses(ClassJsonFile classData) { IList objList = null; foreach (var aclass in classData.Classes) { string className = aclass.ClassName; List <ClassField> classProperties = aclass.ClassFields; //MyObjectBuilder Class MyObjectBuilder o = new MyObjectBuilder(); //Creating a new object dynamically object newObj = o.CreateNewObject(classProperties, classData.AssemblyName, classData.DynamicModuleName); Type listType = typeof(List <>).MakeGenericType(o.objType); objList = (IList)Activator.CreateInstance(listType); Type t = newObj.GetType(); object instance = Activator.CreateInstance(t); PropertyInfo[] props = instance.GetType().GetProperties(); int instancePropsCount = props.Count(); for (int i = 0; i < instancePropsCount; ++i) { string fieldName = props[i].Name; MemberInfo[] mInfo = null; PropertyInfo pInfo = newObj.GetType().GetProperty(fieldName); var prop = classProperties.Find(x => x.Name == fieldName); object value = null; if (prop != null) { value = prop.Value; } if (pInfo != null) { //var value = pInfo.GetValue(newObj, null); mInfo = t.GetMember(fieldName); if (value != null && mInfo != null && !string.IsNullOrEmpty(mInfo[0].ToString())) { if (typeof(ICollection).IsAssignableFrom(((PropertyInfo)mInfo[0]).PropertyType)) { // collection type assign values MyObjectBuilder.SetCollectionMemberValue(mInfo[0], instance, value); } else { MyObjectBuilder.SetMemberValue(mInfo[0], instance, value); } } } else { mInfo = t.GetMember(fieldName); if (mInfo != null && !string.IsNullOrEmpty(mInfo[0].ToString())) { MyObjectBuilder.SetMemberValue(mInfo[0], instance, null); } } } objList.Add(instance); } return(objList); }