Exemplo n.º 1
0
        public void InterfaceSet()
        {
            OopOperator <ITest> interfaceBuilder = new OopOperator <ITest>();

            interfaceBuilder.ClassName("UTestClass");
            interfaceBuilder["MethodWidthReturnInt"]    = "return 123456;";
            interfaceBuilder["MethodWidthReturnString"] = "return \"test\";";
            interfaceBuilder["MethodWidthParamsRefInt"] = "i+=10;";
            interfaceBuilder["MethodWidthParamsString"] = "return str+\"1\";";
            interfaceBuilder["MethodWidthParams"]       = "return a.ToString()+str+b.ToString();";
            interfaceBuilder.Compile();
            var test  = interfaceBuilder.Create("UTestClass");
            int testi = 1;

            test.MethodWidthParamsRefInt(ref testi);
            Assert.Equal(123456, test.MethodWidthReturnInt());
            Assert.Equal("test", test.MethodWidthReturnString());
            Assert.Equal(11, testi);
            Assert.Equal("test1", test.MethodWidthParamsString("test"));
            Assert.Equal("12test12", test.MethodWidthParams(12, "test", 12));
        }
Exemplo n.º 2
0
        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<Program>("Main")
            //    .StaticMethodContent($@"Console.WriteLine(""Hello World!"")")
            //    .Complie<Action<string[]>>();

            Console.WriteLine(FakeMethodOperator.New
                              .UseMethod <TestB>("TestMethod")
                              .StaticMethodContent($@"Console.WriteLine(""Hello World!"")")
                              .Builder()
                              );


            /*
             *   在此之前,你需要右键,选择工程文件,在你的.csproj里面
             *
             *   写上这样一句浪漫的话:
             *
             *      <PreserveCompilationContext>true</PreserveCompilationContext>
             */

            OopOperator <TestAbstract> abstractBuilder = new OopOperator <TestAbstract>();

            abstractBuilder.ClassName("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"";
    }
}";
            //根据脚本创建动态类
            Type type = RuntimeComplier.GetType(text);
            //创建动态类实例代理
            DynamicOperator instance = new DynamicOperator(type);

            if (instance["Name"].StringValue == "111")
            {
                //调用动态委托赋值
                instance["Name"].StringValue = "222";
            }
            Console.WriteLine("===");
            Console.WriteLine(instance["Instance"].OperatorValue["Name"].StringValue);
            //调用动态类
            Console.WriteLine(instance["Name"].StringValue);

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            if (instance["Name"].StringValue == "111")
            {
                //调用动态委托赋值
                instance["Name"].StringValue = "222";
            }
            stopwatch.Stop();
            Console.WriteLine(stopwatch.Elapsed);


            var newInstance = DynamicOperator.GetOperator(type);

            stopwatch.Restart();
            if (newInstance["Name"].StringValue == "111")
            {
                //调用动态委托赋值
                newInstance["Name"].StringValue = "222";
            }
            stopwatch.Stop();
            Console.WriteLine(stopwatch.Elapsed);


            var entity = EntityOperator.Create(type);

            entity.New();
            stopwatch.Restart();
            if (entity["Name"].Get <string>() == "111")
            {
                //调用动态委托赋值
                entity["Name"].Set("222");
            }
            stopwatch.Stop();
            entity.Get("Instance").Set("Name", "haha");
            Console.WriteLine(entity.Get("Instance").Get <string>("Name"));
            Console.WriteLine(stopwatch.Elapsed);

            entity = EntityOperator.Create(type);
            entity.New();
            stopwatch.Restart();
            if (entity["Name"].Get <string>() == "111")
            {
                //调用动态委托赋值
                entity["Name"].Set("222");
            }
            stopwatch.Stop();
            Console.WriteLine("new:" + stopwatch.Elapsed);


            for (int i = 0; i < 10; i++)
            {
                newInstance = DynamicOperator.GetOperator(type);
                stopwatch.Restart();
                if (newInstance["Name"].StringValue == "111")
                {
                    //调用动态委托赋值
                    newInstance["Name"].StringValue = "222";
                }
                stopwatch.Stop();
                Console.WriteLine(stopwatch.Elapsed);
            }



            for (int i = 0; i < 10; i++)
            {
                entity = EntityOperator.Create(type);
                entity.New();
                stopwatch.Restart();
                if (entity["Name"].Get <string>() == "111")
                {
                    //调用动态委托赋值
                    entity["Name"].Set("222");
                }
                stopwatch.Stop();
                Console.WriteLine("new:" + stopwatch.Elapsed);
            }



            for (int i = 0; i < 10; i++)
            {
                newInstance = DynamicOperator.GetOperator(type);
                stopwatch.Restart();
                if (newInstance["Name"].StringValue == "111")
                {
                    //调用动态委托赋值
                    newInstance["Name"].StringValue = "222";
                }
                stopwatch.Stop();
                Console.WriteLine(stopwatch.Elapsed);
            }


            for (int i = 0; i < 10; i++)
            {
                entity = EntityOperator.Create(type);
                entity.New();
                stopwatch.Restart();
                if (entity.Get <string>("Name") == "111")
                {
                    //调用动态委托赋值
                    entity.Set("Name", "222");
                }
                stopwatch.Stop();
                Console.WriteLine("new:" + stopwatch.Elapsed);
            }


            for (int i = 0; i < 10; i++)
            {
                var tEntity = (new TestB()).Caller();
                stopwatch.Restart();
                if (tEntity.Get <string>("Name") == "111")
                {
                    //调用动态委托赋值
                    tEntity.Set("Name", "222");
                }
                stopwatch.Stop();
                Console.WriteLine("new:" + stopwatch.Elapsed);
            }


            //创建动态类实例代理
            DynamicOperator <TestB> instance2 = new DynamicOperator <TestB>();

            if (instance2["Name"].StringValue == "111")
            {
                //调用动态委托赋值
                instance2["Name"].StringValue = "222";
            }
            //调用动态类
            Console.WriteLine(instance2["Name"].StringValue);

            var temp = StaticEntityOperator.Create(typeof(StaticTestB));

            temp.Set("Name", "Name");
            temp.Set("Age", 1);

            Console.WriteLine(temp.Get <string>("Name"));
            Console.WriteLine(temp.Get <int>("Age"));
            Console.WriteLine(StaticTestB.Name);
            Console.WriteLine(StaticTestB.Age);
            Console.ReadKey();
        }
Exemplo n.º 3
0
        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>
             */

            OopOperator <TestAbstract> abstractBuilder = new OopOperator <TestAbstract>();

            abstractBuilder.ClassName("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"";
    }
}";
            //根据脚本创建动态类
            Type type = RuntimeComplier.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();
        }