/// <summary> /// 初始化 Natasha 组件 /// </summary> public static Task Initialize(bool initializeReference = true) { #if DEBUG Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); #endif if (!_hasInitialize) { lock (_lock) { if (!_hasInitialize) { _hasInitialize = true; Task.Run(() => { var host = new AdhocWorkspace(); }); var task1 = Task.Run(() => { NatashaComponentRegister.RegistSyntax <NatashaCSharpSyntax>(); }); var task2 = Task.Run(() => { NatashaComponentRegister.RegistDomain <NatashaAssemblyDomain>(initializeReference); NatashaComponentRegister.RegistCompiler <NatashaCSharpCompiler>(); NatashaCSharpCompiler.AddSemanticAnalysistor(UsingAnalysistor.Creator()); }); Task.WaitAll(task1, task2); } } } #if DEBUG stopwatch.Stop(); Console.WriteLine($"\r\n================ [Regist] \t注册组件总时长: {stopwatch.ElapsedMilliseconds}ms ================ \r\n"); #endif return(Task.CompletedTask); }
static NatashaInitializer() { NatashaComponentRegister.RegistDomain <NatashaAssemblyDomain>(); NatashaComponentRegister.RegistCompiler <NatashaCSharpCompiler>(); NatashaComponentRegister.RegistSyntax <NatashaCSharpSyntax>(); _lock = new object(); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { NatashaComponentRegister.RegistDomain <NatashaAssemblyDomain>(); NatashaComponentRegister.RegistCompiler <NatashaCSharpCompiler>(); NatashaComponentRegister.RegistSyntax <NatashaCSharpSyntax>(); NatashaController.Builder = (ApplicationBuilder)app; if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseSwagger(); // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), // specifying the Swagger JSON endpoint. app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); c.RoutePrefix = string.Empty; }); app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); NatashaController.Endpoints = endpoints.DataSources; }); }
static void Main(string[] args) { NatashaComponentRegister.RegistDomain <NatashaAssemblyDomain>(); NatashaComponentRegister.RegistCompiler <NatashaCSharpCompiler>(); NatashaComponentRegister.RegistSyntax <NatashaCSharpSyntax>(); var action = NDelegate.RandomDomain().Action("Console.WriteLine(\"Hello world!\")"); action(); Console.ReadKey(); }
/// <summary> /// 初始化 Natasha 组件 /// </summary> public static async Task Initialize(bool initializeReference = true) { if (!_hasInitialize) { lock (_lock) { if (!_hasInitialize) { _hasInitialize = true; NatashaComponentRegister.RegistDomain <NatashaAssemblyDomain>(initializeReference); NatashaComponentRegister.RegistCompiler <NatashaCSharpCompiler>(); NatashaComponentRegister.RegistSyntax <NatashaCSharpSyntax>(); } } } }
/// <summary> /// 初始化 Natasha 组件 /// </summary> public static Task Initialize(bool initializeReference = true) { if (!_hasInitialize) { lock (_lock) { if (!_hasInitialize) { _hasInitialize = true; NatashaComponentRegister.RegistDomain <NatashaAssemblyDomain>(initializeReference); NatashaComponentRegister.RegistCompiler <NatashaCSharpCompiler>(); NatashaComponentRegister.RegistSyntax <NatashaCSharpSyntax>(); NatashaCSharpCompiler.AddSemanticAnalysistor(UsingAnalysistor.Creator()); } } } return(Task.CompletedTask); }
static void Main(string[] args) { NatashaComponentRegister.RegistDomain <NatashaAssemblyDomain>(); NatashaComponentRegister.RegistCompiler <NatashaCSharpCompiler>(); NatashaComponentRegister.RegistSyntax <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() { NatashaComponentRegister.RegistDomain <NatashaAssemblyDomain>(); NatashaComponentRegister.RegistCompiler <NatashaCSharpCompiler>(); NatashaComponentRegister.RegistSyntax <NatashaCSharpSyntax>(); }
static CSharpScriptEngine() { NatashaComponentRegister.RegistDomain <NatashaAssemblyDomain>(); NatashaComponentRegister.RegistCompiler <NatashaCSharpCompiler>(); NatashaComponentRegister.RegistSyntax <NatashaCSharpSyntax>(); }
static void Main(string[] args) { NatashaComponentRegister.RegistDomain <NatashaAssemblyDomain>(); NatashaComponentRegister.RegistCompiler <NatashaCSharpCompiler>(); NatashaComponentRegister.RegistSyntax <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(); }
static void Main(string[] args) { NatashaComponentRegister.RegistDomain <NatashaAssemblyDomain>(); NatashaComponentRegister.RegistCompiler <NatashaCSharpCompiler>(); NatashaComponentRegister.RegistSyntax <NatashaCSharpSyntax>(); NSucceedLog.Enabled = true; //List<Student> students = new List<Student>(); //for (int i = 0; i < 5; i++) //{ // students.Add(new Student() // { // Age = i, // Description = "This is 描述!", // Name = "test" + i, // Sex = i % 2 == 0 ? "男" : "女", // Flag = i % 2 == 0 ? (short?)1 : null, // UpdateTime = i % 2 == 0 ? (short?)1000 : null, // }); //} var dict = new Dictionary <string, string> { { "Id", "分类Id" }, { "Pid", "父节点ID" }, { "CategoryName", "分类名称" }, { "CreateTime", "创建时间" }, { "Status", "状态" }, { "UpdateTime", "修改时间" }, { "ViewCount", "浏览量" } }; ExcelOperator.SetWritterMapping <QuestionCategory>(dict, "描述"); //ExcelOperator.WriteToFile("1.xlsx", students); ExcelOperator.SetReaderMapping <QuestionCategory>(dict); var list = ExcelOperator.FileToEntities <QuestionCategory>("2020-08-04 09-08 QuestionCategory.xlsx"); //var a = ExcelOperator<QuestionCategory>.Get("2020-08-04 09-08 QuestionCategory.xlsx", 0); //Invoke(a.Item1, a.Item2); Console.ReadKey(); //using (ExcelFile file = new ExcelFile(AppDomain.CurrentDomain.BaseDirectory + "1.xlsx")) //{ // ExcelStyle style = ExcelStyle.Create(file); // file.LoadTemplate("1.txt") // .UseTemplate("Test2") // .FillHeader(style.Header()) // .FillCollection(students) // .Save(); //} //using (ExcelFile file = new ExcelFile(AppDomain.CurrentDomain.BaseDirectory + "2.xlsx")) //{ // file.Select(1); // //阶梯 // file.CurrentCell(0.ToString()); // for (int i = 1; i < 10; i+=1) // { // file // .NextRow(false) // .NextCell(i.ToString()); // } // file.Save(); //} //using (ExcelOperator file = new ExcelOperator(AppDomain.CurrentDomain.BaseDirectory + "2.xlsx")) //{ // var sheet = file[1]; // //阶梯 // Console.WriteLine(sheet[0][0].StringValue); // for (int i = 1; i < 10; i += 1) // { // Console.WriteLine(sheet[i][i].StringValue); // } //} }
static void Main(string[] args) { NatashaComponentRegister.RegistDomain <NatashaAssemblyDomain>(); NatashaComponentRegister.RegistCompiler <NatashaCSharpCompiler>(); NatashaComponentRegister.RegistSyntax <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(); }
/// <summary> /// 初始化 Natasha 组件 /// </summary> public static void Initialize() { NatashaComponentRegister.RegistDomain <NatashaAssemblyDomain>(); NatashaComponentRegister.RegistCompiler <NatashaCSharpCompiler>(); NatashaComponentRegister.RegistSyntax <NatashaCSharpSyntax>(); }