public static void Show() { var domain = AssemblyManagment.Create("TempDomain"); domain.Unloading += Domain_Unloading; var temp = domain.Execute <FastMethodOperator>(builder => { return(builder //.MethodAttribute<MethodImplAttribute>("MethodImplOptions.NoInlining") .MethodBody(@"Console.WriteLine(""\t动态功能输出:Hello World!"");")); }); action = temp.Complie <Action>(); action(); AssemblyManagment.Get("TempDomain").Dispose(); }
static void Main(string[] args) { Console.WriteLine("隔离编译动态方法:"); Console.WriteLine(); Show(); if (action != null) { Console.WriteLine("\t静态引用动态方法,增加方法代数!"); } //var a = AssmblyManagment.Remove("TempDomain"); Console.WriteLine(); Console.WriteLine(); Console.Write("第一次检测:"); Console.WriteLine(AssemblyManagment.IsDelete("TempDomain") ? "回收成功!" : "回收失败!"); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("启用GC回收方法!"); for (int i = 0; (!AssemblyManagment.IsDelete("TempDomain")) && (i < 15); i++) { Console.WriteLine($"\t第{i}次!"); GC.Collect(); GC.WaitForPendingFinalizers(); Thread.Sleep(500); if (i == 6) { Console.WriteLine($"\t计数为{i},删除静态引用!"); //千万别再这里调用 AssemblyManagment.Get("TempDomain").Dispose(); action = null; } } Console.WriteLine(); Console.WriteLine(); //Console.WriteLine(!a.IsAlive? "回收成功!":"回收失败!"); Console.Write("第二次检测:"); Console.WriteLine(AssemblyManagment.IsDelete("TempDomain") ? "回收成功!" : "回收失败!"); action?.Invoke(); Console.ReadKey(); }
static void Main(string[] args) { /* * 在此之前,你需要右键,选择工程文件,在你的.csproj里面 * * 写上这样一句浪漫的话: * * <PreserveCompilationContext>true</PreserveCompilationContext> */ string text = @"using System; using System.Collections; using System.Linq; using System.Text; using ClassLibrary1; namespace HelloWorld { public class Test { public Test(){ Name=""111""; } public string Name; public int Age{get;set;} public override string ToString(){ Class1 a = new Class1(); a.Show1(); Class1.Show2(); return ""11""; } public static int Get(int temp){ switch (temp) { case 100: temp = 1; break; case 200: temp = 333; break; case 300: temp = 645; break; case 400: temp = 1412; break; case 500: temp = 653; break; case 600: temp = 2988; break; default: temp = 2019; break; } return temp; } } }"; //根据脚本创建动态类 var domain = AssemblyManagment.Create("Default"); domain.LoadFile(@"D:\Project\IlTest\ClassLibrary1\bin\Debug\netstandard2.0\ClassLibrary1.dll"); OopComplier oop = new OopComplier(); oop.Domain = domain; Type type = oop.GetClassType(text); var a = Activator.CreateInstance(type); Console.WriteLine(a.ToString()); Console.ReadKey(); }