public void TestCall1() { //ScriptComplier.Init(); string text = @"using System; using System.Collections; using System.Linq; using System.Text; namespace HelloWorld { public static class StaticTest2 { static StaticTest2(){ Name=""111""; } public static string Name; public static int Age{get;set;} } }"; //根据脚本创建动态类 Type type = (new OopComplier()).GetClassType(text); //创建动态类实例代理 var instance = DictOperator.Create(type); //Get动态调用 Assert.Equal("111", (string)instance["Name"]); //调用动态委托赋值 instance["Name"] = "222"; Assert.Equal("222", (string)instance["Name"]); Assert.Equal("222", instance.Get <string>("Name")); }
public void TestCall6() { //创建动态类实例代理 var instance = DictOperator <TestB> .Create(); instance.New(); Assert.Equal("111", (string)instance["Name"]); //调用动态委托赋值 instance.Set("Name", "222"); Assert.Equal("222", (string)instance["Name"]); var c = (TestC)instance["InstanceC"]; Assert.Equal("abc", c.Name); instance["InstanceC"] = (new TestC() { Name = "bbca" }); Assert.Equal("bbca", ((TestC)instance["InstanceC"]).Name); }
public void TestCall5() { //创建动态类实例代理 var instance = DictOperator <TestB> .Create(); instance.New(); Assert.Equal("111", (string)instance["Name"]); //调用动态委托赋值 instance.Set("Name", "222"); Assert.Equal("222", (string)instance["Name"]); }
public void TestCall3() { //创建动态类实例代理 var instance = DictOperator.Create(typeof(FakeStaticTestModel2)); FakeStaticTestModel2.Name = "111"; Assert.Equal("111", (string)instance["Name"]); instance["Name"] = "222"; Assert.Equal("222", (string)instance["Name"]); FakeStaticTestModel2.Age = 1001; Assert.Equal(1001, (int)instance["Age"]); FakeStaticTestModel2.Temp = DateTime.Now; instance["Temp"] = FakeStaticTestModel2.Temp; Assert.Equal(FakeStaticTestModel2.Temp, (DateTime)instance["Temp"]); }
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 = LinkOperator.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); var dict = DictOperator.Create(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(); }