static void Main(string[] args) { /* * 在此之前,你需要右键,选择工程文件,在你的.csproj里面 * * 写上这样一句浪漫的话: * * <PreserveCompilationContext>true</PreserveCompilationContext> */ string text = @"namespace HelloWorld { public class Test { public Test(){ Name=""111""; } public string Name; public int Age{get;set;} } }"; //根据脚本创建动态类 OopComplier oop = new OopComplier(); Type type = oop.GetClassType(text); Console.ReadKey(); }
public static void RunClassName2() { //ScriptComplier.Init(); string text = @"using System; using System.Collections; using System.Linq; using System.Text; using System.Collections.Generic; namespace HelloWorld { public class TestIndex1 { public string Name; public int Age{get;set;} } public class TestIndex2 { public string Name; public int Age{get;set;} public List<object> ooo; } public class TestIndex3 { public string Name; public int Age{get;set;} } } namespace HelloWorld{ public struct TestStruct1{} public struct TestStruct2{} public class TestIndex4 { public string Name; public int Age{get;set;} } } "; //根据脚本创建动态类 OopComplier oop = new OopComplier(); Type type = oop.GetClassType(text, 1, 2); Assert.Equal("TestIndex4", type.Name); type = oop.GetStructType(text, 2, 2); Assert.Equal("TestStruct2", type.Name); }
static void Main(string[] args) { /* * 在此之前,你需要右键,选择工程文件,在你的.csproj里面 * * 写上这样一句浪漫的话: * * <PreserveCompilationContext>true</PreserveCompilationContext> */ string text = @"using System; using System.Collections; using System.Linq; using System.Text; using ClassLibrary1; namespace HelloWorld { public class Test { public Test(){ Name=""111""; } public string Name; public int Age{get;set;} public override string ToString(){ Class1 a = new Class1(); a.Show1(); Class1.Show2(); return ""11""; } } }"; //根据脚本创建动态类 OopComplier oop = new OopComplier(); oop.LoadFile(@"D:\Project\IlTest\ClassLibrary1\bin\Debug\netstandard2.0\ClassLibrary1.dll"); Type type = oop.GetClassType(text); var a = Activator.CreateInstance(type); Console.WriteLine(a.ToString()); Console.ReadKey(); }
public static void RunClassName0() { //ScriptComplier.Init(); string text = @" namespace HelloWorld {public class Test{public Test(){ Name=""111""; }public string Name; public int Age{get;set;} } }"; //根据脚本创建动态类 OopComplier oop = new OopComplier(); Type type = oop.GetClassType(text); Assert.Equal("Test", type.Name); }
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(); }
public OopBuilder() { Complier = new OopComplier(); }
static void Main(string[] args) { /* * 在此之前,你需要右键,选择工程文件,在你的.csproj里面 * * 写上这样一句浪漫的话: * * <PreserveCompilationContext>true</PreserveCompilationContext> */ string text = @"using System; using System.Collections; using System.Linq; using System.Text; using ClassLibrary1; namespace HelloWorld { public class Test { public Test(){ Name=""111""; } public string Name; public int Age{get;set;} public override string ToString(){ Class1 a = new Class1(); a.Show1(); Class1.Show2(); return ""11""; } public static int Get(int temp){ switch (temp) { case 100: temp = 1; break; case 200: temp = 333; break; case 300: temp = 645; break; case 400: temp = 1412; break; case 500: temp = 653; break; case 600: temp = 2988; break; default: temp = 2019; break; } return temp; } } }"; //根据脚本创建动态类 var domain = AssemblyManagment.Create("Default"); domain.LoadFile(@"D:\Project\IlTest\ClassLibrary1\bin\Debug\netstandard2.0\ClassLibrary1.dll"); OopComplier oop = new OopComplier(); oop.Domain = domain; Type type = oop.GetClassType(text); var a = Activator.CreateInstance(type); Console.WriteLine(a.ToString()); Console.ReadKey(); }