Ejemplo n.º 1
0
        public static void MakerStaticCode2()
        {
            var builder = FakeMethodOperator.Create()
                          .UseMethod(typeof(OopTestModel).GetMethod("ReWrite2"))
                          .StaticMethodContent(@"Console.WriteLine(""hello world"");return this;")
                          .Builder();

            Assert.Equal($@"public static async Task<OopTestModel> ReWrite2()
{{
Console.WriteLine(""hello world"");return this;{Environment.NewLine}
}}", builder.MethodScript);
        }
Ejemplo n.º 2
0
        public static void MakerStaticCode3()
        {
            var builder = FakeMethodOperator.Create();

            builder
            .UseMethod(typeof(OopTestModel).GetMethod("ReWrite3"))
            .StaticMethodContent(@"i++;temp+=i.ToString();")
            .Builder();
            Assert.Equal($@"public static void ReWrite3(ref Int32 i,String temp)
{{
i++;temp+=i.ToString();{Environment.NewLine}
}}", builder.MethodScript);
        }
Ejemplo n.º 3
0
        public static void MakerCode1()
        {
            var builder = FakeMethodOperator.Create();

            builder
            .UseMethod(typeof(OopTestModel).GetMethod("ReWrite1"))
            .MethodContent(@"Console.WriteLine(""hello world"");")
            .Builder();
            Assert.Equal($@"public void ReWrite1()
{{
Console.WriteLine(""hello world"");{Environment.NewLine}
}}", builder.MethodScript);
        }
Ejemplo n.º 4
0
        public static void MakerCode2()
        {
            var builder = FakeMethodOperator.Default();

            builder
            .UseMethod(typeof(OopTestModel).GetMethod("ReWrite2"))
            .MethodContent(@"Console.WriteLine(""hello world"");return this;")
            .Builder();
            Assert.Equal($@"public async System.Threading.Tasks.Task<NatashaUT.Model.OopTestModel> ReWrite2()
{{
Console.WriteLine(""hello world"");return this;{Environment.NewLine}
}}", builder.MethodScript);
        }
Ejemplo n.º 5
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();
        }
Ejemplo n.º 6
0
        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)();
            }
        }