Example #1
0
        public void Precache()
        {
            Type          type   = typeof(CallModel);
            DynamicMethod method = new DynamicMethod("GetString", typeof(string), new Type[] { type });
            ILGenerator   il     = method.GetILGenerator();

            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Ldfld, type.GetField("Age"));
            il.Emit(OpCodes.Ret);
            EmitGetString = (Func <CallModel, string>)(method.CreateDelegate(typeof(Func <CallModel, string>)));


            method = new DynamicMethod("GetDateTime", typeof(DateTime), new Type[] { type });
            il     = method.GetILGenerator();
            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Ldfld, type.GetField("CreateTime"));
            il.Emit(OpCodes.Ret);
            EmitGetDateTime = (Func <CallModel, DateTime>)(method.CreateDelegate(typeof(Func <CallModel, DateTime>)));


            method = new DynamicMethod("SetDateString", null, new Type[] { type, typeof(string) });
            il     = method.GetILGenerator();
            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Ldarg_1);
            il.Emit(OpCodes.Stfld, type.GetField("Age"));
            il.Emit(OpCodes.Ret);
            EmitSetString = (Action <CallModel, string>)(method.CreateDelegate(typeof(Action <CallModel, string>)));


            method = new DynamicMethod("SetDateTime", null, new Type[] { type, typeof(DateTime) });
            il     = method.GetILGenerator();
            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Ldarg_1);
            il.Emit(OpCodes.Stfld, type.GetField("CreateTime"));
            il.Emit(OpCodes.Ret);
            EmitSetDateTime = (Action <CallModel, DateTime>)(method.CreateDelegate(typeof(Action <CallModel, DateTime>)));


            NatashaGetString = NFunc <CallModel, string> .Delegate("return arg.Age;");

            NatashaGetString(OriginModel);
            OriginGetString = item => item.Age;


            NatashaGetDateTime = NFunc <CallModel, DateTime> .Delegate("return arg.CreateTime;");

            NatashaGetDateTime(OriginModel);
            OriginGetDateTime = item => item.CreateTime;


            NatashaSetString = NAction <CallModel, string> .Delegate("arg1.Age=arg2;");

            NatashaSetString(OriginModel, OriginModel.Age);
            OriginSetString = (item, value) => item.Age = value;

            NatashaSetDateTime = NDelegateOperator <ValueDelegate> .Delegate("model.CreateTime=value;");

            NatashaSetDateTime(OriginModel, OriginModel.CreateTime);
            OriginSetDateTime = OriginDateTime;
        }
Example #2
0
 public static Action UnsafeAsyncDelegate(string content, params NamespaceConverter[] usings)
 {
     return(content == default ? null : NDelegateOperator <Action> .UnsafeAsyncDelegate(content, usings));
 }
Example #3
0
 public static T Create <T>(this T instance, string content, params NamespaceConverter[] usings) where T : Delegate
 {
     return(instance = NDelegateOperator <T> .Delegate(content, usings));
 }
Example #4
0
 public static Func <T> Delegate(string content, params NamespaceConverter[] usings)
 {
     return(content == default? null : NDelegateOperator <Func <T> > .Delegate(content, usings));
 }
Example #5
0
        static void Main(string[] args)
        {
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }
            var action = FastMethodOperator.MainDomain
                         .MethodBody("return 1+1;")
                         .Return <int>()
                         .Complie <Func <int> >();

            action();


            FakeMethodOperator.MainDomain
            .UseMethod <TestB>("TestMethod")
            .StaticMethodContent($@"Console.WriteLine(""Hello World!"");")
            .Complie <Action>();


            FakeMethodOperator.MainDomain
            .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 = NDelegateOperator <GetterDelegate> .Delegate("return value.ToString();");

            Console.WriteLine(delegate2(1));
            var delegate3     = "return value.ToString();".Delegate <GetterDelegate>();
            var delegateConvt = FastMethodOperator.MainDomain
                                .Param <string>("value")
                                .MethodBody($@"return value==""true"" || value==""mama"";")
                                .Return <bool>()
                                .Complie <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.MainDomain
                               .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();
        }
Example #6
0
 public static T Delegate <T>(this string instance, params NamespaceConverter[] types) where T : Delegate
 {
     return(NDelegateOperator <T> .Delegate(instance, types));
 }