/// <summary> /// Find all the tasks which are available from the loaded assemblies /// </summary> /// <param name="TaskNameToReflectionInfo">Mapping from task name to information about how to serialize it</param> /// <param name="bPublicTasksOnly">Whether to include just public tasks, or all the tasks in any loaded assemblies</param> static bool FindAvailableTasks(Dictionary <string, ScriptTask> NameToTask, bool bPublicTasksOnly) { Assembly[] LoadedAssemblies = AppDomain.CurrentDomain.GetAssemblies(); if (bPublicTasksOnly) { LoadedAssemblies = LoadedAssemblies.Where(x => IsPublicAssembly(new FileReference(x.Location))).ToArray(); } foreach (Assembly LoadedAssembly in LoadedAssemblies) { Type[] Types = LoadedAssembly.GetTypes(); foreach (Type Type in Types) { foreach (TaskElementAttribute ElementAttribute in Type.GetCustomAttributes <TaskElementAttribute>()) { if (!Type.IsSubclassOf(typeof(CustomTask))) { CommandUtils.LogError("Class '{0}' has TaskElementAttribute, but is not derived from 'Task'", Type.Name); return(false); } if (NameToTask.ContainsKey(ElementAttribute.Name)) { CommandUtils.LogError("Found multiple handlers for task elements called '{0}'", ElementAttribute.Name); return(false); } NameToTask.Add(ElementAttribute.Name, new ScriptTask(ElementAttribute.Name, Type, ElementAttribute.ParametersType)); } } } return(true); }
public void _LoadAssembly() { var odf = new OpenFileDialog(); odf.ShowDialog(); AssemblyPath = odf.FileName; if (string.IsNullOrEmpty(odf.FileName)) { return; } LoadedAssembly = Assembly.LoadFile(AssemblyPath); foreach (Type t in LoadedAssembly.GetTypes()) { if (t.BaseType == (typeof(DbContext))) { DBEntitiesType = t; break; } } EFTypes = new ObservableCollection <Type>(); foreach (var prop in DBEntitiesType.GetProperties()) { Type type = prop.PropertyType; if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(DbSet <>)) { EFTypes.Add(type.GetGenericArguments()[0]); } } // EFTypes = new ObservableCollection<Type>(LoadedAssembly.GetTypes().Where(x => x.Name != dbEntitiesType .Name).ToList()); }
public void _GenMock() { using (DbContext myContext = Activator.CreateInstance(DBEntitiesType) as DbContext) { try { var tableType = LoadedAssembly .GetTypes().FirstOrDefault(t => t.Name == SelectedEntityName); var method = myContext.GetType().GetMethods() .First(x => x.IsGenericMethod && x.Name == "Set"); MethodInfo generic = method.MakeGenericMethod(tableType); var set = generic.Invoke(myContext, null); DbSet a = myContext.Set(tableType); var rr = a.SqlQuery(RequestSQL).ToListAsync().Result; var props = tableType.GetProperties(); string outPut = ""; foreach (var elm in rr) { outPut += "new " + tableType.Name + "(){\n"; foreach (var prop in props) { object value = prop.GetValue(elm, null); // against prop.Name Type type = prop.PropertyType; string name = prop.Name; // against prop.Name if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(ICollection <>)) { continue; } if (type == typeof(int) || type == typeof(int?)) { if (value != null) { outPut += '@' + name + "=" + value + ",\n"; } else { outPut += '@' + name + "=null,\n"; } } else if (type == typeof(string)) { outPut += '@' + name + "=\"" + value + "\",\n"; } else if (type == typeof(decimal) || type == typeof(decimal?)) { if (value != null) { decimal val = (decimal)value; outPut += '@' + name + "=" + val.ToString(CultureInfo.InvariantCulture) + "m,\n"; } else { outPut += '@' + name + "=null,\n"; } } else if (type == typeof(DateTime) || type == typeof(DateTime?)) { if (value != null) { DateTime aaa = (DateTime)value; // var a = new DateTime(aaa.Year, aaa.Month, aaa.Day, aaa.Hour, aaa.Minute, aaa.Second); var dateString = $"{aaa.Year},{aaa.Month},{aaa.Day},{aaa.Hour},{aaa.Minute},{aaa.Second}"; outPut += '@' + name + "=new DateTime(" + dateString + "),\n"; } else { outPut += '@' + name + "=null,\n"; } } else if (type == typeof(bool) || type == typeof(bool?)) { if (value != null) { outPut += '@' + name + "=" + value.ToString().ToLower() + ",\n"; } else { outPut += '@' + name + "=null,\n"; } } } outPut += "};\n"; Console.WriteLine(outPut); } OutPutSrc = outPut; } catch (Exception ex) { OutPutSrc = ex.Message; } } }
static int Main(string[] args) { VisualMode = false; HTMLMode = false; FileName = ""; TypeName = null; if (args.Length == 0) { bool can_not_exit = true; while (can_not_exit) { Console.Write("ShavedIce_ClassViewer " + FileName + "$ "); string[] cmd = Console.ReadLine().SplitDoubleQuotation(); Console.WriteLine(cmd.ToStringAll(",")); if (cmd.Length == 0) { continue; } switch (cmd[0]) { case "writeconsole": case "print": { foreach (var type in LoadedAssembly.GetTypes()) { InstanceWriter.WriteToConsole(type); } } break; case "writefile": { if (cmd.Length == 2) { using (StreamWriter sw = new StreamWriter(cmd[1])) { foreach (var type in LoadedAssembly.GetTypes()) { InstanceWriter.WriteToTextFile(sw, type); } } } else { Console.WriteLine("ARG COUNT IS WRONG"); } } break; case "writehtml": { if (cmd.Length == 2) { StringBuilder sb = new StringBuilder(); foreach (var type in LoadedAssembly.GetTypes()) { sb.AppendLine(InstanceWriter.GetHTMLText(type)); } InstanceWriter.WriteHTML(cmd[1], LoadedAssembly.FullName, sb.ToString()); System.Diagnostics.Process.Start(cmd[1]); } else { Console.WriteLine("ARG COUNT IS WRONG"); } } break; case "load": { if (cmd.Length == 2) { try { LoadedAssembly = Assembly.LoadFile(cmd[1]); } catch (Exception e) { Console.WriteLine("CAN'T LOAD FILE\r\n" + e.ToString()); } } else { Console.WriteLine("ARG COUNT IS WRONG"); } } break; case "exit": { can_not_exit = false; } break; default: continue; } } return(0); } try { LoadedAssembly = Assembly.LoadFrom(args[0]); } catch (Exception e) { Console.WriteLine("ERROR 0002 : CAN'T LOAD ASSET FILE"); return(-1); } for (int i = 1; i < args.Length; i++) { switch (args[i]) { // Visual Mode case "-v": { VisualMode = true; } break; // Output File case "-f": { i++; if (i == args.Length) { Console.WriteLine("ERROR 0003 : FILE OUTPUT COMMAND IS WRONG"); return(-1); } else { FileName = args[i]; } } break; // Type Name case "-t": { i++; if (i == args.Length) { Console.WriteLine("ERROR 0004 : TYPE COMMAND IS WRONG"); return(-1); } else { TypeName = args[i]; } } break; // HTML case "-html": { HTMLMode = true; } break; default: { Console.WriteLine("ERROR 0005 : ARGS AREN'T EXIST"); } return(-1); } } if (VisualMode) { Application.Run(new VisualMode()); } else { ConsoleMode.ConsoleMain(); } return(0); }