Nextrek
@ HEAD
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Web.Script.Serialization; using Microsoft.CSharp; using System.CodeDom.Compiler; namespace ProjectCaporal { namespace ActorNS { class Caporal { private static string url = "http://192.168.1.70/coordSTUB/prova.json"; private static Actor actor; static void Main(string[] args) { run(null); } static void run(string[] args) { Task<string> taskPippo2 = GetAsync(url); string pippo2 = taskPippo2.Result.ToString(); try { JavaScriptSerializer temp = new JavaScriptSerializer(); actor = temp.Deserialize<Actor>(pippo2); Console.WriteLine(actor.Status); Console.WriteLine(actor.Result.DataFileName); } catch (Exception e) { Console.WriteLine("Error " + e.Message); } string urlCode = actor.Result.ActionFilePosition + actor.Result.ActionFileName; Task<string> taskCode = GetAsync(urlCode); string remoteCode = taskCode.Result.ToString(); string[] parameters = actor.Result.Parameters; int numParams = parameters.Length; string[] args3 = new string[numParams]; for (int i = 0; i < numParams; i++) { args3[i] = parameters[i]; } Console.WriteLine(remoteCode); try { ExecuteCode(remoteCode, "ActorNS", "ActorObj", "run", false, args3); } catch (Exception e) { Console.WriteLine("Error2 " + e.Message); } Console.ReadKey(); } public static async Task<string> GetAsync(string uri) { HttpClient httpClient = new HttpClient(); HttpResponseMessage response = await httpClient.GetAsync(uri); //will throw an exception if not successful response.EnsureSuccessStatusCode(); string content = await response.Content.ReadAsStringAsync(); return content; } private static MemoryStream GenerateStreamFromString(string value) { return new MemoryStream(Encoding.UTF8.GetBytes(value ?? "")); } private static Assembly BuildAssembly(string code) { Microsoft.CSharp.CSharpCodeProvider provider = new CSharpCodeProvider(); ICodeCompiler compiler = provider.CreateCompiler(); CompilerParameters compilerparams = new CompilerParameters(); compilerparams.GenerateExecutable = false; compilerparams.GenerateInMemory = true; compilerparams.ReferencedAssemblies.Add("C:\\Program Files (x86)\\Reference Assemblies\\Microsoft\\Framework\\.NETFramework\\v4.5.2\\System.dll"); compilerparams.ReferencedAssemblies.Add("C:\\Program Files (x86)\\Reference Assemblies\\Microsoft\\Framework\\.NETFramework\\v4.5.2\\System.Configuration.dll"); compilerparams.ReferencedAssemblies.Add("C:\\Program Files (x86)\\Reference Assemblies\\Microsoft\\Framework\\.NETFramework\\v4.5.2\\System.Data.dll"); compilerparams.ReferencedAssemblies.Add("C:\\Program Files (x86)\\Reference Assemblies\\Microsoft\\Framework\\.NETFramework\\v4.5.2\\System.Data.DataSetExtensions.dll"); compilerparams.ReferencedAssemblies.Add("C:\\Program Files (x86)\\Reference Assemblies\\Microsoft\\Framework\\.NETFramework\\v4.5.2\\System.Net.Http.dll"); // compilerparams.ReferencedAssemblies.Add("C:\\Program Files (x86)\\Reference Assemblies\\Microsoft\\Framework\\.NETFramework\v4.5.2\\System.Runtime.Serialization.dll"); compilerparams.ReferencedAssemblies.Add("C:\\Program Files (x86)\\Reference Assemblies\\Microsoft\\Framework\\.NETFramework\\v4.5.2\\System.Web.Extensions.dll"); CompilerResults results = compiler.CompileAssemblyFromSource(compilerparams, code); if (results.Errors.HasErrors) { StringBuilder errors = new StringBuilder("Compiler Errors :\r\n"); foreach (CompilerError error in results.Errors) { errors.AppendFormat("Line {0},{1}\t: {2}\n", error.Line, error.Column, error.ErrorText); } throw new Exception(errors.ToString()); } else { return results.CompiledAssembly; } } public static object ExecuteCode(string code, string namespacename, string classname, string functionname, bool isstatic, params string[] args) { object returnval = null; Assembly asm = BuildAssembly(code); object instance = null; Type type = null; if (isstatic) { type = asm.GetType(namespacename + "." + classname); } else { instance = asm.CreateInstance(namespacename + "." + classname); type = instance.GetType(); } MethodInfo method = type.GetMethod(functionname); returnval = method.Invoke(instance, args); return returnval; } } class Actor { public string Status { get; set; } public string ErrorCode { get; set; } public string ErrorMessage { get; set; } public ActorResult Result { get; set; } } public class ActorResult { public string DataFileName { get; set; } public string DataFilePosition { get; set; } public string ActionFileName { get; set; } public string ActionFilePosition { get; set; } public string[] Parameters { get; set; } } } } |