Beispiel #1
0
        public static void MakerStaticCode2()
        {
            var builder = FakeMethodOperator.RandomDomain()
                          .UseMethod(typeof(OopTestModel).GetMethod("ReWrite2"))
                          .StaticMethodBody(@"Console.WriteLine(""hello world"");return default;");

            Assert.Equal($@"public static async System.Threading.Tasks.Task<NatashaUT.Model.OopTestModel> ReWrite2(){{Console.WriteLine(""hello world"");return default;}}", builder.Script);
            Assert.NotNull(builder.Compile());
        }
Beispiel #2
0
        public static void MakerCode3()
        {
            var builder = FakeMethodOperator.RandomDomain();

            builder
            .UseMethod(typeof(OopTestModel).GetMethod("ReWrite3"))
            .Methodbody(@"i++;temp+=i.ToString();");
            Assert.Equal($@"public virtual void ReWrite3(ref System.Int32 i,System.String temp){{i++;temp+=i.ToString();}}", builder.Script);
        }
Beispiel #3
0
        public static void MakerCode2()
        {
            var builder = FakeMethodOperator.RandomDomain();

            builder
            .UseMethod(typeof(OopTestModel).GetMethod("ReWrite2"))
            .Methodbody(@"Console.WriteLine(""hello world"");return this;");
            Assert.Equal($@"public async System.Threading.Tasks.Task<NatashaUT.Model.OopTestModel> ReWrite2(){{Console.WriteLine(""hello world"");return this;}}", builder.Script);
        }
Beispiel #4
0
        public static void MakerCode1()
        {
            var builder = FakeMethodOperator.RandomDomain();

            builder
            .UseMethod(typeof(OopTestModel).GetMethod("ReWrite1"))
            .Methodbody(@"Console.WriteLine(""hello world"");");
            Assert.Equal($@"public void ReWrite1(){{Console.WriteLine(""hello world"");}}", builder.Script);
        }
Beispiel #5
0
        public static void MakerStaticCode3()
        {
            var builder = FakeMethodOperator.RandomDomain();

            builder
            .UseMethod(typeof(OopTestModel).GetMethod("ReWrite3"))
            .StaticMethodBody(@"i++;temp+=i.ToString();");
            Assert.Equal($@"public static void ReWrite3(ref System.Int32 i,System.String temp){{i++;temp+=i.ToString();}}", builder.Script);
            Assert.NotNull(builder.Compile <TestDelegate>());
        }
Beispiel #6
0
        public void MakerStaticCode4()
        {
            var builder = FakeMethodOperator.RandomDomain();

            builder
            .UseMethod(typeof(OopTestModel).GetMethod("ReWrite4"))
            .StaticMethodBody(@"temp = default;return ref i;");
            Assert.Equal($@"public static ref System.Int32 ReWrite4(ref System.Int32 i,out System.String temp){{temp = default;return ref i;}}", builder.Script);
            Assert.NotNull(builder.Compile <TestDelegate1>());
        }
Beispiel #7
0
        public void MakerStaticCode1()
        {
            var builder = FakeMethodOperator.RandomDomain(item => item.LogCompilerError());

            builder
            .UseMethod(typeof(OopTestModel).GetMethod("ReWrite1"))
            .StaticMethodBody(@"Console.WriteLine(""hello world"");");
            Assert.Equal($@"public static void ReWrite1(){{Console.WriteLine(""hello world"");}}", builder.Script);
            Assert.NotNull(builder.Compile());
        }
Beispiel #8
0
        static void Main(string[] args)
        {
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }
            var action = FastMethodOperator.DefaultDomain()
                         .Body("return 1+1;")
                         .Return <int>()
                         .Compile <Func <int> >();

            action();


            FakeMethodOperator.RandomDomain(item => {
                item.AssemblyName = "1";
                item.OutputToFile = true;
            })
            .UseMethod <TestB>("TestMethod")
            .StaticMethodBody($@"Console.WriteLine(""Hello World!"");")
            .Compile <Action>();


            FakeMethodOperator.RandomDomain(item => {
                item.AssemblyName = "1";
                item.OutputToFile = true;
            })
            .UseMethod <TestB>("TestMethod")
            .MethodBody($@"Console.WriteLine(""Hello World!"");")
            .Compile <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.CreateProxy("UTestClass");

            //var delegate2 = NDelegateOperator<GetterDelegate>.Delegate("return value.ToString();");
            //Console.WriteLine(delegate2(1));
            //var delegate3 = "return value.ToString();".Delegate<GetterDelegate>();
            //var delegateConvt = FastMethodOperator.Create()
            //    .Param<string>("value")
            //    .MethodBody($@"return value==""true"" || value==""mama"";")
            //    .Return<bool>()
            //    .Compile<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.DefaultDomain()
                               .Param <TestB>("instance")
                               .Param <string>("value")
                               .Body("instance.Name = value;")
                               .Compile <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 DynamicCacheBuilder(IDictionary <TKey, TValue> pairs, DyanamicCacheDirection queryDirection = DyanamicCacheDirection.Both)
        {
            if (queryDirection != DyanamicCacheDirection.ValueToKey)
            {
                AnonymousRTD _r2d_handler = AnonymousRTD.UseDomain(typeof(TKey).GetDomain());
                var          key_builder  = new Dictionary <TKey, string>();
                foreach (var item in pairs)
                {
                    key_builder[item.Key] = $"return {_r2d_handler.AddValue(item.Value)};";
                }


                StringBuilder keyBuilder = new StringBuilder();
                keyBuilder.Append(ScriptKeyAction(key_builder));
                keyBuilder.Append("return default;");


                var method = typeof(Func <TKey, TValue>).GetMethod("Invoke");
                _r2d_handler.BodyAppend(FakeMethodOperator.RandomDomain()
                                        .UseMethod(method)
                                        .Unsafe()
                                        .StaticMethodBody(keyBuilder.ToString())
                                        .Script);
                var type = _r2d_handler.Complie();


                GetValue = NDelegate
                           .UseDomain(typeof(TKey).GetDomain())
                           .Func <Func <TKey, TValue> >($"return {_r2d_handler.TypeName}.Invoke;", type)();
            }



            if (queryDirection != DyanamicCacheDirection.KeyToValue)
            {
                AnonymousRTD _r2d_handler  = AnonymousRTD.UseDomain(typeof(TKey).GetDomain());
                var          value_builder = new Dictionary <TValue, string>();
                foreach (var item in pairs)
                {
                    if (!value_builder.ContainsKey(item.Value))
                    {
                        value_builder[item.Value] = $"return new {typeof(TKey).GetDevelopName()}[]{{{_r2d_handler.AddValue(item.Key)}";
                    }
                    else
                    {
                        value_builder[item.Value] += $",{_r2d_handler.AddValue(item.Key)}";
                    }
                }

                var temp_value_buidler = new Dictionary <TValue, string>();
                foreach (var item in value_builder)
                {
                    temp_value_buidler[item.Key] = item.Value + "};";
                }


                StringBuilder valueBuilder = new StringBuilder();
                valueBuilder.Append(ScriptValueAction(temp_value_buidler));
                valueBuilder.Append("return null;");


                var method = typeof(Func <TValue, TKey[]>).GetMethod("Invoke");
                _r2d_handler.Body(FakeMethodOperator.RandomDomain()
                                  .UseMethod(method)
                                  .Unsafe()
                                  .StaticMethodBody(valueBuilder.ToString())
                                  .Script);
                var type = _r2d_handler.Complie();


                GetKeys = NDelegate
                          .UseDomain(typeof(TValue).GetDomain())
                          .Func <Func <TValue, TKey[]> >($"return {_r2d_handler.TypeName}.Invoke;", type)();
            }
        }