Advertisement
Effectsprod

Untitled

Jan 22nd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.52 KB | None | 0 0
  1. ====================
  2. public static void CompileAndRun(string code)
  3.         {
  4.        
  5.  
  6.             CompilerParameters CompilerParams = new CompilerParameters();
  7.             string outputDirectory = Directory.GetCurrentDirectory();
  8.  
  9.             CompilerParams.GenerateInMemory = true;
  10.            
  11.             CompilerParams.TreatWarningsAsErrors = false;
  12.             CompilerParams.GenerateExecutable = true;
  13.  
  14.  
  15.             CompilerParams.CompilerOptions = "/optimize";
  16.  
  17.             string[] references = { "System.dll", "System.Windows.Forms.dll", "System.Drawing.dll", "xNet.dll", "Bunifu_UI_v1.5.3.dll", "System.ComponentModel.dll" };
  18.             CompilerParams.ReferencedAssemblies.AddRange(references);
  19.  
  20.  
  21.             CSharpCodeProvider provider = new CSharpCodeProvider();
  22.  
  23.             CompilerResults compile = provider.CompileAssemblyFromSource(CompilerParams, code);
  24.  
  25.             if (compile.Errors.HasErrors)
  26.             {
  27.                 string text = "Compile error: ";
  28.                 foreach (CompilerError ce in compile.Errors)
  29.                 {
  30.                     text += "rn" + ce.ToString();
  31.                 }
  32.                 throw new Exception(text);
  33.             }
  34.  
  35.             ExpoloreAssembly(compile.CompiledAssembly);
  36.  
  37.             Module module = compile.CompiledAssembly.GetModules()[0];
  38.             Type mt = null;
  39.             MethodInfo methInfo = null;
  40.  
  41.             if (module != null)
  42.             {
  43.                 mt = module.GetType("DynaCore.DynaCore");
  44.             }
  45.  
  46.             if (mt != null)
  47.             {
  48.                 methInfo = mt.GetMethod("Main");
  49.             }
  50.  
  51.             if (methInfo != null)
  52.             {
  53.                 Console.WriteLine(methInfo.Invoke(null, new object[] { "here in dyna code" }));
  54.             }
  55.         }
  56.  
  57.         static void ExpoloreAssembly(Assembly assembly)
  58.         {
  59.             Console.WriteLine("Modules in the assembly:");
  60.             foreach (Module m in assembly.GetModules())
  61.             {
  62.                 Console.WriteLine("{0}", m);
  63.  
  64.                 foreach (Type t in m.GetTypes())
  65.                 {
  66.                     Console.WriteLine("t{0}", t.Name);
  67.  
  68.                     foreach (MethodInfo mi in t.GetMethods())
  69.                     {
  70.                         Console.WriteLine("tt{0}", mi.Name);
  71.                     }
  72.                 }
  73.             }
  74.  
  75. ====================
  76.  
  77. Код который хочу выполнить
  78. --
  79. MessageBox.Show("test");
  80. CompileAndRun("MessageBox.Show(\"test\");");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement