static void Main(string[] args) { if (args == null) { throw new ArgumentNullException(nameof(args)); } var action = FastMethodOperator.New .MethodBody("return 1+1;") .Return <int>() .Complie <Func <int> >(); action(); FakeMethodOperator.New .UseMethod <TestB>("TestMethod") .StaticMethodContent($@"Console.WriteLine(""Hello World!"");") .Complie <Action>(); FakeMethodOperator.New .UseMethod <TestB>("TestMethod") .MethodContent($@"Console.WriteLine(""Hello World!"");") .Complie <Action>(new TestA()); /* * 在此之前,你需要右键,选择工程文件,在你的.csproj里面 * * 写上这样一句浪漫的话: * * <PreserveCompilationContext>true</PreserveCompilationContext> */ OopOperator <TestAbstract> abstractBuilder = new OopOperator <TestAbstract>(); abstractBuilder.OopName("UTestClass"); abstractBuilder["GetName"] = "return Name;"; abstractBuilder["GetAge"] = "return Age;"; abstractBuilder.Compile(); var test = abstractBuilder.Create("UTestClass"); var delegate2 = DelegateOperator <GetterDelegate> .Create("return value.ToString();"); Console.WriteLine(delegate2(1)); var delegate3 = "return value.ToString();".Create <GetterDelegate>(); var delegateConvt = FastMethodOperator.New .Param <string>("value") .MethodBody($@"return value==""true"" || value==""mama"";") .Return <bool>() .Complie <Func <string, bool> >(); Console.WriteLine(delegateConvt("mama")); string text = @"using System; using System.Collections; using System.Linq; using System.Text; namespace HelloWorld { public class Test { public Test(){ Name=""111""; Instance = new Test1(); } public string Name; public int Age{get;set;} public Test1 Instance; } public class Test1{ public string Name=""1""; } }"; //根据脚本创建动态类 Type type = RuntimeComplier.GetClassType(text); DynamicMethod method = new DynamicMethod("GetString", null, new Type[] { typeof(TestB), typeof(string) }); ILGenerator il = method.GetILGenerator(); il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldarg_1); il.Emit(OpCodes.Stfld, typeof(TestB).GetField("Name")); il.Emit(OpCodes.Ret); var emitAction = (Action <TestB, string>)(method.CreateDelegate(typeof(Action <TestB, string>))); var roslynAction = FastMethodOperator.New .Param <TestB>("instance") .Param <string>("value") .MethodBody("instance.Name = value;") .Return() .Complie <Action <TestB, string> >(); Stopwatch stopwatch = new Stopwatch(); stopwatch.Restart(); for (int i = 0; i < 50000; i++) { var tEntity = new TestB(); roslynAction(tEntity, "abc"); } stopwatch.Stop(); Console.WriteLine("Roslyn:\t" + stopwatch.Elapsed); stopwatch.Restart(); for (int i = 0; i < 50000; i++) { var tEntity = new TestB(); emitAction(tEntity, "abc"); } stopwatch.Stop(); Console.WriteLine("Emit:\t" + stopwatch.Elapsed); stopwatch.Restart(); for (int i = 0; i < 50000; i++) { var tEntity = new TestB(); roslynAction(tEntity, "abc"); } stopwatch.Stop(); Console.WriteLine("Roslyn:\t" + stopwatch.Elapsed); stopwatch.Restart(); for (int i = 0; i < 50000; i++) { var tEntity = new TestB(); emitAction(tEntity, "abc"); } stopwatch.Stop(); Console.WriteLine("Emit:\t" + stopwatch.Elapsed); Console.ReadKey(); }
static void Main(string[] args) { Stopwatch stopwatch = new Stopwatch(); for (int j = 0; j < 20; j++) { Console.WriteLine("========================================="); stopwatch.Restart(); for (int i = 0; i < 50000; i++) { var tEntity = new TestB(); if (tEntity.Name == "111") { //调用动态委托赋值 tEntity.Name = "222"; } } stopwatch.Stop(); Console.WriteLine("原生调用:\t\t" + stopwatch.Elapsed); var entity = DynamicCaller.Create(typeof(TestB)); stopwatch.Restart(); for (int i = 0; i < 50000; i++) { entity.New(); if (entity.Get <string>("Name") == "111") { //调用动态委托赋值 entity.Set("Name", "222"); } } stopwatch.Stop(); Console.WriteLine("NCaller SimpleCaller:\t" + stopwatch.Elapsed); stopwatch.Restart(); for (int i = 0; i < 50000; i++) { RunDynamic(new TestB()); } stopwatch.Stop(); Console.WriteLine("Dynamic :\t\t" + stopwatch.Elapsed); stopwatch.Restart(); for (int i = 0; i < 50000; i++) { var tEntity = (new TestB()).Caller(); if (tEntity.Get <string>("Name") == "111") { //调用动态委托赋值 tEntity.Set("Name", "222"); } } stopwatch.Stop(); Console.WriteLine("NCaller Extension:\t" + stopwatch.Elapsed); Console.WriteLine("========================================="); } Console.ReadKey(); }
static void Main(string[] args) { TempTime = DateTime.Now; Stopwatch stopwatch = new Stopwatch(); for (int j = 0; j < 20; j++) { Console.WriteLine("========================================="); stopwatch.Restart(); for (int i = 0; i < 40000; i++) { var tEntity = new TestB(); if (tEntity.A2ge712 == "111") { //调用动态委托赋值 tEntity.A2ge712 = "222"; } } stopwatch.Stop(); Console.WriteLine("原生调用:\t\t" + stopwatch.Elapsed); var entity = DynamicCaller.Create(typeof(TestB)); stopwatch.Restart(); for (int i = 0; i < 40000; i++) { entity.New(); if (entity.Get <string>("A2ge712") == "111") { //调用动态委托赋值 entity.Set("A2ge712", "222"); } } stopwatch.Stop(); Console.WriteLine("NCaller SimpleCaller:\t" + stopwatch.Elapsed); stopwatch.Restart(); for (int i = 0; i < 40000; i++) { RunDynamic(new TestB()); } stopwatch.Stop(); Console.WriteLine("Dynamic :\t\t" + stopwatch.Elapsed); stopwatch.Restart(); for (int i = 0; i < 40000; i++) { var tEntity = (new TestB()).Caller(); if (tEntity.Get <string>("A2ge712") == "111") { //调用动态委托赋值 tEntity.Set("A2ge712", "222"); } } stopwatch.Stop(); Console.WriteLine("NCaller Extension:\t" + stopwatch.Elapsed); //entity = DynamicCaller.Create(typeof(TestB)); //stopwatch.Restart(); //for (int i = 0; i < 40000; i++) //{ // entity.New(); // if (entity.Get<DateTime>("Time") != TempTime) // { // //调用动态委托赋值 // entity.Set("Time", TempTime); // } //} //stopwatch.Stop(); //Console.WriteLine("NCaller SimpleCaller:\t" + stopwatch.Elapsed); //stopwatch.Restart(); //for (int i = 0; i < 40000; i++) //{ // RunDynamicTime(new TestB()); //} //stopwatch.Stop(); //Console.WriteLine("Dynamic :\t\t" + stopwatch.Elapsed); //entity = DynamicCaller.Create(typeof(TestB)); //stopwatch.Restart(); //for (int i = 0; i < 40000; i++) //{ // entity.New(); // if (entity.Get<DateTime>("Time") != TempTime) // { // //调用动态委托赋值 // entity.Set("Time", TempTime); // } //} //stopwatch.Stop(); //Console.WriteLine("NCaller SimpleCaller:\t" + stopwatch.Elapsed); Console.WriteLine("========================================="); } Console.ReadKey(); }
static void Main(string[] args) { if (args == null) { throw new ArgumentNullException(nameof(args)); } var action = FastMethodOperator.Default() .Body("return 1+1;") .Return <int>() .Compile <Func <int> >(); action(); FakeMethodOperator.Default() .UseMethod <TestB>("TestMethod") .StaticMethodBody($@"Console.WriteLine(""Hello World!"");") .Compile <Action>(); FakeMethodOperator.Default() .UseMethod <TestB>("TestMethod") .Methodbody($@"Console.WriteLine(""Hello World!"");") .Compile <Action>(new TestA()); /* * 在此之前,你需要右键,选择工程文件,在你的.csproj里面 * * 写上这样一句浪漫的话: * * <PreserveCompilationContext>true</PreserveCompilationContext> */ //ProxyOperator<TestAbstract> abstractBuilder = new ProxyOperator<TestAbstract>(); //abstractBuilder.OopName("UTestClass"); //abstractBuilder["GetName"] = "return Name;"; //abstractBuilder["GetAge"] = "return Age;"; //abstractBuilder.Compile(); //var test = abstractBuilder.CreateProxy("UTestClass"); //var delegate2 = NDelegateOperator<GetterDelegate>.Delegate("return value.ToString();"); //Console.WriteLine(delegate2(1)); //var delegate3 = "return value.ToString();".Delegate<GetterDelegate>(); //var delegateConvt = FastMethodOperator.Create() // .Param<string>("value") // .MethodBody($@"return value==""true"" || value==""mama"";") // .Return<bool>() // .Compile<Func<string, bool>>(); //Console.WriteLine(delegateConvt("mama")); DynamicMethod method = new DynamicMethod("GetString", null, new Type[] { typeof(TestB), typeof(string) }); ILGenerator il = method.GetILGenerator(); il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldarg_1); il.Emit(OpCodes.Stfld, typeof(TestB).GetField("Name")); il.Emit(OpCodes.Ret); var emitAction = (Action <TestB, string>)(method.CreateDelegate(typeof(Action <TestB, string>))); var roslynAction = FastMethodOperator.Default() .Param <TestB>("instance") .Param <string>("value") .Body("instance.Name = value;") .Compile <Action <TestB, string> >(); Stopwatch stopwatch = new Stopwatch(); stopwatch.Restart(); for (int i = 0; i < 50000; i++) { var tEntity = new TestB(); roslynAction(tEntity, "abc"); } stopwatch.Stop(); Console.WriteLine("Roslyn:\t" + stopwatch.Elapsed); stopwatch.Restart(); for (int i = 0; i < 50000; i++) { var tEntity = new TestB(); emitAction(tEntity, "abc"); } stopwatch.Stop(); Console.WriteLine("Emit:\t" + stopwatch.Elapsed); stopwatch.Restart(); for (int i = 0; i < 50000; i++) { var tEntity = new TestB(); roslynAction(tEntity, "abc"); } stopwatch.Stop(); Console.WriteLine("Roslyn:\t" + stopwatch.Elapsed); stopwatch.Restart(); for (int i = 0; i < 50000; i++) { var tEntity = new TestB(); emitAction(tEntity, "abc"); } stopwatch.Stop(); Console.WriteLine("Emit:\t" + stopwatch.Elapsed); Console.ReadKey(); }
static void Main(string[] args) { NatashaInitializer.InitializeAndPreheating(); TempTime = DateTime.Now; Stopwatch stopwatch = new Stopwatch(); for (int j = 0; j < 20; j++) { Console.WriteLine("========================================="); stopwatch.Restart(); for (int i = 0; i < 40000; i++) { var tEntity = new TestB(); if (tEntity.A2ge712 == "111") { //调用动态委托赋值 tEntity.A2ge712 = "222"; } } stopwatch.Stop(); Console.WriteLine("原生调用:\t\t" + stopwatch.Elapsed); //var entity = LinkOperator.CreateFromType(typeof(TestB)); //stopwatch.Restart(); //for (int i = 0; i < 40000; i++) //{ // entity.New(); // if (entity.Get<string>("A2ge712") == "111") // { // //调用动态委托赋值 // entity.Set("A2ge712", "222"); // } //} //stopwatch.Stop(); //Console.WriteLine("NCaller SimpleCaller:\t" + stopwatch.Elapsed); var dict = PrecisionDictOperator.CreateFromType(typeof(TestB)); stopwatch.Restart(); for (int i = 0; i < 40000; i++) { dict.New(); if ((string)(dict["A2ge712"]) == "111") { //调用动态委托赋值 dict["A2ge712"] = "222"; } } stopwatch.Stop(); Console.WriteLine("NCaller DictCaller:\t" + stopwatch.Elapsed); stopwatch.Restart(); for (int i = 0; i < 40000; i++) { RunDynamic(new TestB()); } stopwatch.Stop(); Console.WriteLine("Dynamic :\t\t" + stopwatch.Elapsed); //stopwatch.Restart(); //for (int i = 0; i < 40000; i++) //{ // var tEntity = (new TestB()).LinkCaller(); // if (tEntity.Get<string>("A2ge712") == "111") // { // //调用动态委托赋值 // tEntity.Set("A2ge712", "222"); // } //} //stopwatch.Stop(); //Console.WriteLine("NCaller Extension:\t" + stopwatch.Elapsed); //entity = DynamicCaller.Create(typeof(TestB)); //stopwatch.Restart(); //for (int i = 0; i < 40000; i++) //{ // entity.New(); // if (entity.Get<DateTime>("Time") != TempTime) // { // //调用动态委托赋值 // entity.Set("Time", TempTime); // } //} //stopwatch.Stop(); //Console.WriteLine("NCaller SimpleCaller:\t" + stopwatch.Elapsed); //stopwatch.Restart(); //for (int i = 0; i < 40000; i++) //{ // RunDynamicTime(new TestB()); //} //stopwatch.Stop(); //Console.WriteLine("Dynamic :\t\t" + stopwatch.Elapsed); //entity = DynamicCaller.Create(typeof(TestB)); //stopwatch.Restart(); //for (int i = 0; i < 40000; i++) //{ // entity.New(); // if (entity.Get<DateTime>("Time") != TempTime) // { // //调用动态委托赋值 // entity.Set("Time", TempTime); // } //} //stopwatch.Stop(); //Console.WriteLine("NCaller SimpleCaller:\t" + stopwatch.Elapsed); Console.WriteLine("========================================="); } //var dict = DictOperator<TestB>.Create(); //dict["Name"] = "Hello"; //dict["Age"] = 100; //dict["Time"] = DateTime.Now; Console.ReadKey(); }