public void NormalSet() { ProxyOperator <NTest> builder = new ProxyOperator <NTest>(); builder.OopName("NTestClass"); builder["MethodWidthReturnInt"] = "return 123456;"; builder["MethodWidthReturnString"] = "return \"test\";"; builder["MethodWidthParamsRefInt"] = "i+=10;"; builder["MethodWidthParamsString"] = "return str+\"1\";"; builder["MethodWidthParams"] = "b=10;return a.ToString()+str+b.ToString();"; builder.Compile(); dynamic test = Activator.CreateInstance(builder.TargetType); int testi = 1; int param3; test.MethodWidthParamsRefInt(ref testi); Assert.Equal("NTestClass", builder.TargetType.GetDevelopName()); Assert.Equal(123456, test.MethodWidthReturnInt()); Assert.Equal("test", test.MethodWidthReturnString()); Assert.Equal(11, testi); Assert.Equal("test1", test.MethodWidthParamsString("test")); Assert.Equal("12test10", test.MethodWidthParams(12, "test", out param3)); Assert.Equal(10, param3); }
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> */ ProxyOperator <TestAbstract> abstractBuilder = new ProxyOperator <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""; } }"; OopComplier oop = new OopComplier(); //根据脚本创建动态类 Type type = oop.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(); }