HATEBIN
>
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.CSharp; using System.CodeDom.Compiler; using System.Reflection; using System.Runtime.Serialization.Formatters.Binary; using System.Net; using System.IO; using System.Linq.Expressions; namespace RuntimeCompilationTest { public class Program { public static CSharpCodeProvider provider = new CSharpCodeProvider(); public static CompilerParameters parameters = new CompilerParameters(); public static int myint = 1542; public static List
mylist = new List
(); public static List
methods = new List
(); static void Main(string[] args) { parameters.GenerateInMemory = true; parameters.ReferencedAssemblies.Add("System.dll"); parameters.ReferencedAssemblies.Add( typeof(Program).Assembly.Location); CompilerResults results = provider.CompileAssemblyFromSource(parameters, GetCode()); var cls = results.CompiledAssembly.GetType("DynamicNS.DynamicCode"); MethodInfo method = cls.GetMethod("DynamicMethod", BindingFlags.Static | BindingFlags.Public); int x = 10; object o = (object)x; object[] m = new object[1]; m[0] = o; method.Invoke(null, m); Console.WriteLine(mylist.FirstOrDefault().ToLower()); methods.Add(method); SerializeThisShit SS = new SerializeThisShit(); SS.methodlist = methods; SS.Serialize(@"methods.bin"); try{ if (File.Exists(@"methods.bin")) { //New instance to be in new memspace for this SerializeThisShit MM = new SerializeThisShit(); MM.Deserialize(@"methods.bin"); //try to invoke the method in the deserialized state int y = 32; object p = (object)y; object[] l = new object[1]; l[0] = p; //MM.methodlist.First().Invoke(null, l); } } catch(Exception ex) { //Error catching. Console.WriteLine(ex); } System.Threading.Thread.Sleep(10000); } static string[] GetCode() { return new string[] { @"using System; using RuntimeCompilationTest; namespace DynamicNS { public static class DynamicCode { public static void DynamicMethod(int l) { Console.WriteLine(""Hello, world!""); Console.WriteLine(Program.myint); Program.mylist.Add(""TADA!!!""); } } }" }; } } [Serializable] public class SerializeThisShit { public List
methodlist = new List
(); public object l = new object(); public void Serialize(string filepath) { System.IO.Stream stream = null; try { this.l = (object)methodlist; stream = System.IO.File.Open(filepath, System.IO.FileMode.Create); BinaryFormatter bformatter = new BinaryFormatter(); bformatter.Serialize(stream, this.l); stream.Close(); stream.Dispose(); } catch (Exception ex) { try { stream.Close(); stream.Dispose(); } catch (Exception) { } throw new Exception(ex.Message); } } public void Deserialize(string filepath) { System.IO.Stream stream = null; try { stream = System.IO.File.Open(filepath, System.IO.FileMode.Open); BinaryFormatter bformatter = new BinaryFormatter(); this.l = (object)bformatter.Deserialize(stream); this.methodlist = (List
)this.l; stream.Close(); stream.Dispose(); } catch (Exception ex) { try { Console.WriteLine(ex); stream.Close(); stream.Dispose(); } catch (Exception ey) { Console.WriteLine(ey); } } } } }