示例#1
0
        public static void Show1()
        {
            string result;
            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Lib", "Sql", "ClassLibrary1.dll");

            using (DomainManagement.CreateAndLock("TempDomain11"))
            {
                var domain    = DomainManagement.CurrentDomain;
                var assemebly = domain.LoadPluginFromStream(path);
                var action    = FastMethodOperator.DefaultDomain()
                                .Using(assemebly)
                                .Body(@"
try{
Class1 a = new Class1();
return  a.Show();
}
catch(Exception e){
    return e.Message;
}
return default;").Return <string>()

                                .Compile <Func <string> >();
                result = action();
                Console.WriteLine(result);
            }
        }
示例#2
0
        public static void RunInnerDelegate()
        {
            var delegateAction = FastMethodOperator
                                 .DefaultDomain()
                                 .Body(@"
                           OopTestModel.InnerClass a = new OopTestModel.InnerClass();
                            a.Name =""abc"";
                            return a;")
                                 .Return <OopTestModel.InnerClass>()
                                 .Compile();

            var result = ((Func <OopTestModel.InnerClass>)delegateAction)?.Invoke();

            Assert.Equal("abc", result.Name);
        }
示例#3
0
        public async void RunAsyncDelegate1()
        {
            var delegateAction = FastMethodOperator.DefaultDomain()

                                 .Async()
                                 .Body(@"
                            await Task.Delay(100);
                            string result = arg1 +"" ""+ arg2;
                            return result;")

                                 .Compile <Func <string, string, Task <string> > >();

            string result = await delegateAction?.Invoke("Hello", "World2!");

            Assert.Equal("Hello World2!", result);
        }
示例#4
0
        public static void RunDelegate2()
        {
            var delegateAction = FastMethodOperator
                                 .DefaultDomain()
                                 .Param <string>("str1")
                                 .Param <string>("str2")
                                 .Body(@"
                            string result = str1 +"" ""+ str2;
                            Console.WriteLine(result);
                            return result;")
                                 .Return <string>()
                                 .Compile <Func <string, string, string> >();

            string result = delegateAction?.Invoke("Hello", "World2!");

            Assert.Equal("Hello World2!", result);
        }
示例#5
0
        internal string ForTest1()
        {
            var domain   = DomainManagement.Create("SingleDomainAsmTest1");
            var assembly = domain.CreateAssembly("AsmTest1");

            var @interface = assembly
                             .CreateInterface("IAsmT1")
                             .Using("System")
                             .Public()
                             .Body("string ShowMethod(string str);");


            var @class = assembly
                         .CreateClass("ClassAsm")
                         .Using("System")
                         .Public()
                         .Inheritance("IAsmT1")
                         .Method(method => method
                                 .Public()
                                 .DefinedName("ShowMethod")
                                 .Param <string>("str")
                                 .Body("return str+AsmEnum.Test.ToString();")
                                 .Return <string>()
                                 );

            var @enum = assembly
                        .CreateEnum("AsmEnum")
                        .Public()
                        .EnumField("Test")
                        .EnumField("Test1")
                        .EnumField("Test2", 1);

            var result = assembly.GetAssembly();
            var type   = assembly.GetTypeFromFullName(@class.NamespaceScript + "." + "ClassAsm");

            var builder = FastMethodOperator.DefaultDomain();

            builder.AssemblyBuilder.Compiler.Domain = domain;
            var @delegate = builder.Using(type).Body(@"
            ClassAsm obj = new ClassAsm();
            return obj.ShowMethod(""Hello"");
            ").Compile <Func <string, string> >();

            DomainManagement.Get("SingleDomainAsmTest1").Dispose();
            return(@delegate("hello"));
        }
示例#6
0
        public void Test3()
        {
            var builder = FastMethodOperator.DefaultDomain();

            builder.AssemblyBuilder.Syntax.ErrorBehavior = ExceptionBehavior.None;
            var delegateAction = builder
                                 .Param <string>("str1")
                                 .Param <string>("str2")
                                 .Body(@"
                            string result = str1 +"" ""+ str2;
                            Console.WriteLine(result)1;
                            return result;")
                                 .Return <string>()
                                 .Compile();

            Assert.Null(delegateAction);
            Assert.Equal(ExceptionKind.Syntax, builder.Exception.ErrorFlag);
        }
示例#7
0
        public async void RunAsyncDelegate2()
        {
            var delegateAction = FastMethodOperator.DefaultDomain()

                                 .Async()
                                 .Param <string>("str1")
                                 .Param <string>("str2")
                                 .Body(@"
                            await Task.Delay(1000);
                            string result = str1 +"" ""+ str2;
                            return result;")
                                 .Return <Task <string> >()

                                 .Compile();

            string result = await((Func <string, string, Task <string> >)delegateAction)?.Invoke("Hello", "World1!");

            Assert.Equal("Hello World1!", result);
        }
示例#8
0
        public static void Test()
        {
            for (int i = 0; i < count; i++)
            {
                var ad      = DomainManagement.Create("test" + i.ToString());
                var builder = FastMethodOperator.DefaultDomain();
                builder.AssemblyBuilder.Compiler.Domain = ad;
                func[i] = builder.Body($@"
int[] a = new int[40960];
for(int i =0;i<40960;i++){{a[i]=i;}}
string temp = @""111111111111111111111111111111111111111111111111111111111111111111111111
2222222222222222222222222222222222223333333333333333333333333333
2222222222222222222222222
44444444444444444444444444444444444444444444444444444444444444444444
555555555555555555555555555555555555555555555555555555555555
6666666666666666666666666666666666666666666666666666666666
77777777777777777777777777777777777777777777722221111111111111111111111111111111"";
Console.WriteLine(""HelloWorld111111111111111111111111111111111111111!"");
return a;
").Compile <Func <int[]> >();
                func[i]();
            }
        }
示例#9
0
        public void Test2()
        {
            var domain   = DomainManagement.Create("SingleDomainAsmTest2");
            var assembly = domain.CreateAssembly("AsmTest1");

            var @interface = assembly
                             .CreateInterface("IAsmT1")
                             .Using("System")
                             .Public()
                             .Body("string ShowMethod(string str);");


            var @class = assembly
                         .CreateClass("ClassAsm")
                         .Using("System")
                         .Public()
                         .Inheritance("IAsmT1")
                         .Method(method => method
                                 .Public()
                                 .DefinedName("ShowMethod")
                                 .Param <string>("str")
                                 .Body("return str+AsmEnum.Test.ToString();")
                                 .Return <string>()
                                 );


            var @enum = assembly
                        .CreateEnum("AsmEnum")
                        .Public()
                        .EnumField("Test")
                        .EnumField("Test1")
                        .EnumField("Test2", 1);


            var result = assembly.GetAssembly();
            var type   = assembly.GetTypeFromFullName(@class.NamespaceScript + "." + "ClassAsm");

            domain.Remove(type.Assembly);


            assembly   = domain.CreateAssembly("AsmTest2");
            @interface = assembly
                         .CreateInterface("IAsmT1")
                         .Using("System")
                         .Public()
                         .Body("string ShowMethod(string str);");


            @class = assembly
                     .CreateClass("ClassAsm")
                     .Using("System")
                     .Public()
                     .Inheritance("IAsmT1")
                     .Method(method => method
                             .Public()
                             .DefinedName("ShowMethod")
                             .Param <string>("str")
                             .Body("return str+AsmEnum.Test.ToString()+\"1\";")
                             .Return <string>()
                             );


            @enum = assembly
                    .CreateEnum("AsmEnum")
                    .Public()
                    .EnumField("Test")
                    .EnumField("Test1")
                    .EnumField("Test2", 1);


            result = assembly.GetAssembly();
            type   = assembly.GetTypeFromFullName(@class.NamespaceScript + ".ClassAsm");



            var builder = FastMethodOperator.DefaultDomain();

            builder.AssemblyBuilder.Compiler.Domain = domain;
            var @delegate = builder.Using(type).Body(@"
ClassAsm obj = new ClassAsm();
return obj.ShowMethod(""Hello"");
").Compile <Func <string, string> >();

            Assert.Equal("HelloTest1", @delegate("hello"));
#if !NETCOREAPP2_2
            @delegate.DisposeDomain();
            for (int i = 0; i < 10; i++)
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }

            Assert.True(DomainManagement.IsDeleted("SingleDomainAsmTest1"));
#endif
        }
示例#10
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();
        }
示例#11
0
        public static void Show()
        {
            //默认共享域
            NStruct nStruct = new NStruct();
            //nStruct
            //    .Namespace("Core30")
            //    .OopName("Test")
            //    .Ctor(builder => builder
            //        .PublicMember
            //        .Param<string>("name")
            //        .Body("Name=name;"))
            //    .PublicField<string>("Name");
            //var type = nStruct.GetType();



            var domain1 = DomainManagement.Create("TempDomain");

            // nStruct = new NStruct();
            // nStruct

            //     .Namespace("Core30")
            //     .OopName("Test1")
            //     .Ctor(builder => builder
            //         .PublicMember
            //         .Param<string>("name")
            //         .Body(@"Name=name+""1"";"))
            //     .PublicField<string>("Name");
            //var type1 = nStruct.GetType();
            // domain1.RemoveType(type1);

            // nStruct = new NStruct();
            // nStruct

            //     .Namespace("Core30")
            //     .OopName("Test")
            //     .Ctor(builder => builder
            //         .PublicMember
            //         .Param<string>("name")
            //         .Body(@"Name=name+""2"";"))
            //     .PublicField<string>("Name");
            // var type3 = nStruct.GetType();
            // domain1.RemoveType(type3);


            nStruct = new NStruct();
            nStruct.AssemblyBuilder.Compiler.Domain = domain1;
            nStruct

            .Namespace("Core30")
            .Name("Test")
            .Ctor(builder => builder
                  .Public()
                  .Param <string>("name")
                  .Body(@"Name=name+""3"";"))
            .PublicField <string>("Name");
            var type4 = nStruct.GetType();


            //nStruct = new NStruct();
            //nStruct
            //    .INDelegate(domain1)
            //    .Namespace("Core30")
            //    .OopName("Test")
            //    .Ctor(builder => builder
            //        .PublicMember
            //        .Param<string>("name")
            //        .Body(@"Name=name+""1"";"))
            //    .PublicField<string>("Name");
            //var type2 = nStruct.GetType();



            var temp = FastMethodOperator.DefaultDomain();

            temp.AssemblyBuilder.Compiler.Domain = domain1;
            temp
            //.Using<Test>()
            //.Using(type)
            .Using(type4)
            //.MethodAttribute<MethodImplAttribute>("MethodImplOptions.NoInlining")
            .Body(@"
Test obj = new Test(""Hello World!"");
Console.WriteLine(obj.Name);"
                  );
            action = temp.Compile <Action>();
            action();
            DomainManagement.Get("TempDomain").Dispose();
        }