private byte[] LogicalOperator(byte[] first, byte[] second, DelegateOperator delegateOperator) { var result = new byte[0]; var lengthResult = MaxValue(first.Length, second.Length); for (var i = 0; i < lengthResult; i++) result = AddToArray(ref result, delegateOperator(GetElementFromPosition(first, i), GetElementFromPosition(second, i))); return result; }
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> .Delegate("return value.ToString();"); Console.WriteLine(delegate2(1)); var delegate3 = "return value.ToString();".Delegate <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 static T CreateUsingStrings <T>(this string instance, params string[] usings) where T : Delegate { return(DelegateOperator <T> .CreateUsingStrings(instance, usings)); }
public static T Create <T>(this T instance, string content, params NamespaceConverter[] usings) where T : Delegate { return(instance = DelegateOperator <T> .Delegate(content, usings)); }
public static T Create <T>(this string instance, params Type[] types) where T : Delegate { return(DelegateOperator <T> .Create(instance, types)); }