static void Main(string[] args) { ComponentRegister.RegistDomain <NatashaAssemblyDomain>(); ComponentRegister.RegistCompiler <NatashaCSharpCompiler>(); ComponentRegister.RegisteSyntax <NatashaCSharpSyntax>(); //var hwFunc = FastMethodOperator // .RandomDomain() // .Param(typeof(string), "str1") // .Param<string>("str2") // .Body("return str1+str2;") // .Return<string>() // .Compile<Func<string, string, string>>(); //Console.WriteLine(hwFunc("Hello", " World!")); var a123 = NClass.UseDomain(typeof(Program).GetDomain()); var domain = DomainManagement.Random; var type = NDelegate.UseDomain(domain, item => item.AssemblyName = "a").GetType($"[assembly: AssemblyKeyFileAttribute(\"c:\\\\vs2019\\\\natasha.snk\")]" + "[assembly: AssemblyVersion(\"1.3.3.3\")]public class A{ public A(){Name=\"1\"; }public string Name;}"); var func = NDelegate.UseDomain(domain).Func <string>("return (new A()).Name;"); Console.WriteLine(type.FullName); Console.WriteLine(func()); //type.RemoveReferences(); type = NDelegate.UseDomain(domain, item => item.AssemblyName = "a").GetType($"[assembly: AssemblyKeyFileAttribute(\"c:\\\\vs2019\\\\natasha.snk\")]" + "[assembly: AssemblyVersion(\"2.3.3.4\")]public class A{ public A(){Name=\"2\"; }public string Name;}"); func = NDelegate.UseDomain(domain).Func <string>("return (new A()).Name;"); Console.WriteLine(type.FullName); Console.WriteLine(func()); domain = DomainManagement.Create("a"); using (DomainManagement.Lock("a")) { Console.WriteLine(domain == (NatashaAssemblyDomain)AssemblyLoadContext.CurrentContextualReflectionContext); } Console.ReadKey(); }
static PrepareTest() { ComponentRegister.RegistDomain <NatashaAssemblyDomain>(); ComponentRegister.RegistCompiler <NatashaCSharpCompiler>(); ComponentRegister.RegisteSyntax <NatashaCSharpSyntax>(); }
static void Main(string[] args) { ComponentRegister.RegistDomain <NatashaAssemblyDomain>(); ComponentRegister.RegistCompiler <NatashaCSharpCompiler>(); ComponentRegister.RegisteSyntax <NatashaCSharpSyntax>(); NErrorLog.Enabled = false; NSucceedLog.Enabled = false; NWarningLog.Enabled = false; Stopwatch watch = new Stopwatch(); double tempTotleTime; #region Natasha Preheating var preTime = Process.GetCurrentProcess().TotalProcessorTime; watch.Start(); tempType = NClass.CreateDomain("tes1t") .Namespace("Test") .UseRandomName() .PublicField <string>("Name") .PublicField <string>("Age") .PublicField <int[]>("Temp") .Ctor(item => item.Body("Temp = new int[40960];")) .GetType(); watch.Stop(); tempTotleTime = GetCpu(Process.GetCurrentProcess().TotalProcessorTime, preTime); if (watch.Elapsed.Seconds > 0) { tempTotleTime = tempTotleTime / watch.Elapsed.Seconds; } Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Magenta; Console.WriteLine("Natasha预热:"); Console.WriteLine("-----------------------------------------------------------------------------------------"); Console.WriteLine($"|\tCPU:{tempTotleTime.ToString("f2")}%\t|\t内存:{Process.GetCurrentProcess().PrivateMemorySize64 / 1024 / 1024}M\t|\t执行耗时:{watch.Elapsed}\t|"); Console.WriteLine("-----------------------------------------------------------------------------------------"); #endregion Thread.Sleep(1000); #region Run Compiler preTime = Process.GetCurrentProcess().TotalProcessorTime; watch.Restart(); Test(); watch.Stop(); tempTotleTime = GetCpu(Process.GetCurrentProcess().TotalProcessorTime, preTime); if (watch.Elapsed.Seconds > 0) { tempTotleTime = tempTotleTime / watch.Elapsed.Seconds; } Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"{count}个独立域编译后:"); Console.WriteLine("-----------------------------------------------------------------------------------------"); Console.WriteLine($"|\tCPU:{tempTotleTime.ToString("f2")}%\t|\t内存:{Process.GetCurrentProcess().PrivateMemorySize64 / 1024 / 1024}M\t|\t执行耗时:{watch.Elapsed}\t|"); Console.WriteLine("-----------------------------------------------------------------------------------------"); #endregion Thread.Sleep(1000); #region Release Handler preTime = Process.GetCurrentProcess().TotalProcessorTime; watch.Restart(); Release(); watch.Stop(); tempTotleTime = GetCpu(Process.GetCurrentProcess().TotalProcessorTime, preTime); if (watch.Elapsed.Seconds > 0) { tempTotleTime = tempTotleTime / watch.Elapsed.Seconds; } Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("释放中:"); Console.WriteLine("-----------------------------------------------------------------------------------------"); Console.WriteLine($"|\tCPU:{tempTotleTime.ToString("f2")}%\t|\t内存:{Process.GetCurrentProcess().PrivateMemorySize64 / 1024 / 1024}M\t|\t执行耗时:{watch.Elapsed}\t|"); Console.WriteLine("-----------------------------------------------------------------------------------------"); #endregion Thread.Sleep(1000); #region Run GC preTime = Process.GetCurrentProcess().TotalProcessorTime; watch.Restart(); RunGc(); watch.Stop(); tempTotleTime = GetCpu(Process.GetCurrentProcess().TotalProcessorTime, preTime); if (watch.Elapsed.Seconds > 0) { tempTotleTime = tempTotleTime / watch.Elapsed.Seconds; } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(); Console.WriteLine("回收后:"); Console.WriteLine("-----------------------------------------------------------------------------------------"); Console.WriteLine($"|\tCPU:{tempTotleTime.ToString("f2")}%\t|\t内存:{Process.GetCurrentProcess().PrivateMemorySize64 / 1024 / 1024}M\t|\t执行耗时:{watch.Elapsed}\t|"); Console.WriteLine("-----------------------------------------------------------------------------------------"); #endregion Thread.Sleep(1000); #region Check Alive preTime = Process.GetCurrentProcess().TotalProcessorTime; watch.Restart(); var alive = CheckAlive(); DomainManagement.Clear(); watch.Stop(); tempTotleTime = GetCpu(Process.GetCurrentProcess().TotalProcessorTime, preTime); if (watch.Elapsed.Seconds > 0) { tempTotleTime = tempTotleTime / watch.Elapsed.Seconds; } Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine(); Console.WriteLine($"存活检测: {alive}"); Console.WriteLine("-----------------------------------------------------------------------------------------"); Console.WriteLine($"|\tCPU:{tempTotleTime.ToString("f2")}%\t|\t内存:{Process.GetCurrentProcess().PrivateMemorySize64 / 1024 / 1024}M\t|\t执行耗时:{watch.Elapsed}\t|"); Console.WriteLine("-----------------------------------------------------------------------------------------"); #endregion //for (int i = 0; i < 10; i++) //{ // Thread.Sleep(3000); // preTime = Process.GetCurrentProcess().TotalProcessorTime; // Console.WriteLine($"第{i}次静默检测:"); // Console.WriteLine("-----------------------------------------------------------------------------------------"); // Console.WriteLine($"|\tCPU:{tempTotleTime.ToString("f2")}%\t|\t内存:{Process.GetCurrentProcess().PrivateMemorySize64 / 1024 / 1024}M\t|\t执行耗时:{watch.Elapsed}\t|"); // Console.WriteLine("-----------------------------------------------------------------------------------------"); //} Console.ReadKey(); }
static void Main(string[] args) { ComponentRegister.RegistDomain <NatashaAssemblyDomain>(); ComponentRegister.RegistCompiler <NatashaCSharpCompiler>(); ComponentRegister.RegisteSyntax <NatashaCSharpSyntax>(); //var @operator = FastMethodOperator.DefaultDomain(); //var actionDelegate = @operator // .Param(typeof(string), "parameter") // .Body("Console.WriteLine(parameter);") // .Compile(); //actionDelegate.DynamicInvoke("HelloWorld!"); //var action = (Action<string>)actionDelegate; //action("HelloWorld!"); //actionDelegate.DisposeDomain(); //起个类 NClass nClass = NClass.DefaultDomain(); nClass .Namespace("MyNamespace") .Public() .Name("MyClass") .Ctor(ctor => ctor.Public().Body("MyField=\"Hello\";")) .Property(prop => prop .Type(typeof(string)) .Name("MyProperty") .Public() .OnlyGetter("return \"World!\";") ); //添加方法 MethodBuilder mb = new MethodBuilder(); mb .Public() .Override() .Name("ToString") .Body("return MyField+\" \"+MyProperty;") .Return(typeof(string)); nClass.Method(mb); //添加字段 FieldBuilder fb = nClass.GetFieldBuilder(); fb.Public() .Name("MyField") .Type <string>(); //动态调用动态创建的类 var action = NDelegate .RandomDomain() .Action("Console.WriteLine((new MyClass()).ToString());", nClass.GetType()); action(); action.DisposeDomain(); //Console.WriteLine(typeof(List<int>[]).GetRuntimeName()); //Console.WriteLine(typeof(List<int>[,]).GetRuntimeName()); //Console.WriteLine(typeof(int[,]).GetRuntimeName()); //Console.WriteLine(typeof(int[][]).GetRuntimeName()); //Console.WriteLine(typeof(int[][,,,]).GetRuntimeName()); Console.ReadKey(); }