Esempio n. 1
0
        public string GetResult1()
        {
            string result;
            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Lib", "Sql", "ClassLibrary1.dll");

            Assert.True(File.Exists(path));
            using (DomainManagment.CreateAndLock("TempDADomain11"))
            {
                var domain    = DomainManagment.CurrentDomain;
                var assemebly = domain.LoadStream(path);
                var action    = FastMethodOperator.Default()
                                .Using(assemebly)
                                .MethodBody(@"
try{
Class1 a = new Class1();
return  a.Show();
}
catch(Exception e){
    Console.WriteLine(e.Message);  
    return e.Message;
}
return default;").Return <string>()

                                .Complie <Func <string> >();
                result = action();
                domain.Dispose();
            }
            return(result);
        }
Esempio n. 2
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.Default()
                                .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);
            }
        }
Esempio n. 3
0
        public static void RunInnerDelegate()
        {
            var delegateAction = FastMethodOperator
                                 .Default()
                                 .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);
        }
Esempio n. 4
0
        public static async void RunAsyncDelegate1()
        {
            var delegateAction = FastMethodOperator.Default()

                                 .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);
        }
Esempio n. 5
0
        internal string ForTest1()
        {
            var domain   = DomainManagment.Create("SingleDomainAsmTest1");
            var assembly = domain.CreateAssembly("AsmTest1");

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


            var @class = assembly
                         .CreateClass("ClassAsm")
                         .Using("System")
                         .Public
                         .Inheritance("IAsmT1")
                         .Method(method => method
                                 .PublicMember
                                 .Name("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.Complier();
            var type   = assembly.GetType(@class.NamespaceScript + "." + "ClassAsm");

            var builder = FastMethodOperator.Default();

            builder.Complier.Domain = domain;
            var @delegate = builder.Using(type).MethodBody(@"
            ClassAsm obj = new ClassAsm();
            return obj.ShowMethod(""Hello"");
            ").Complie <Func <string, string> >();

            DomainManagment.Get("SingleDomainAsmTest1").Dispose();
            return(@delegate("hello"));
        }
Esempio n. 6
0
        public void Test3()
        {
            var builder        = FastMethodOperator.Default();
            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(CompileError.Syntax, builder.Compiler.CompileException.ErrorFlag);
            Assert.Equal(CompileError.Syntax, builder.Compiler.SyntaxExceptions[0].ErrorFlag);
        }
Esempio n. 7
0
        public static void RunDelegate2()
        {
            var delegateAction = FastMethodOperator
                                 .Default()
                                 .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);
        }
Esempio n. 8
0
        public void Test3()
        {
            var builder = FastMethodOperator.Default();

            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);
        }
Esempio n. 9
0
        public static async void RunAsyncDelegate1()
        {
            var delegateAction = FastMethodOperator
                                 .Default()
                                 .UseAsync()
                                 .Param <string>("str1")
                                 .Param <string>("str2")
                                 .MethodBody(@"
                            string result = str1 +"" ""+ str2;
                            Console.WriteLine(result);
                            return result;")
                                 .Return <Task <string> >()
                                 .Complie();

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

            Assert.Equal("Hello World1!", result);
        }
Esempio n. 10
0
        public static async void RunAsyncDelegate2()
        {
            var delegateAction = FastMethodOperator.Default()

                                 .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);
        }
Esempio n. 11
0
        public void TestHelloWorld()
        {
            using (DomainManagment.CreateAndLock("MyDomain"))
            {
                var domain   = DomainManagment.CurrentDomain;
                var assembly = domain.CreateAssembly("MyAssembly");


                //创建一个接口
                assembly
                .CreateInterface("InterfaceTest")
                .Using("System")
                .Public
                .OopBody("string ShowMethod(string str);");


                //创建一个类并实现​接口
                var nameSpace = assembly
                                .CreateClass("TestClass​")
                                .Using("System")
                                .Public
                                .Inheritance("InterfaceTest")
                                .Method(method => method
                                        .PublicMember
                                        .Name("ShowMethod")
                                        .Param <string>("str")
                                        .Body("return str+\" World!\";")
                                        .Return <string>()).NamespaceScript;

                var result = assembly.Complier();
                var type   = assembly.GetType(nameSpace + ".TestClass");

                //单独创建一个程序集​方法
                var func = FastMethodOperator.Default()
                           .Using(type)
                           .MethodBody(@"
TestClass obj = new TestClass​();
return obj.ShowMethod(arg);")
                           .Complie <Func <string, string> >();
                Assert.Equal("Hello World!", func("Hello"));
            }
        }
Esempio n. 12
0
        public string GetResult3()
        {
            string result;
            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Lib", "Json", "ClassLibrary6.dll");

            Assert.True(File.Exists(path));
            using (DomainManagment.CreateAndLock("TempDomain13"))
            {
                var domain    = DomainManagment.CurrentDomain;
                var assemebly = domain.LoadStream(path);
                var action    = FastMethodOperator.Default()
                                .Using(assemebly)
                                .MethodBody(@"Class1 obj = new Class1(); return obj.Get();")
                                .Complie <Func <string> >();
                result = action();
                domain.Dispose();
                domain.Unload();
            }
            return(result);
        }
Esempio n. 13
0
        public string GetResult2()
        {
            string result;
            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Lib", "Static", "ClassLibrary5.dll");

            Assert.True(File.Exists(path));
            using (DomainManagment.CreateAndLock("TempDomain12"))
            {
                var domain    = DomainManagment.CurrentDomain;
                var assemebly = domain.LoadFile(path);
                var action    = FastMethodOperator.Default()
                                .Using(assemebly)
                                .MethodBody(@"Test.Instance.Name=""11""; return Test.Instance.Name;")
                                .Complie <Func <string> >();
                result = action();
                domain.Dispose();
                domain.Unload();
            }
            return(result);
        }
Esempio n. 14
0
        public static void Test()
        {
            for (int i = 0; i < count; i++)
            {
                var ad      = DomainManagement.Create("test" + i.ToString());
                var builder = FastMethodOperator.Default();
                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]();
            }
        }
Esempio n. 15
0
        static void Main(string[] args)
        {
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }
            var action = FastMethodOperator.Default()
                         .MethodBody("return 1+1;")
                         .Return <int>()
                         .Complie <Func <int> >();

            action();


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


            FakeMethodOperator.Default()
            .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.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>()
            //    .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.Default()
                               .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();
        }
Esempio n. 16
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 = DomainManagment.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.Compiler.Domain = domain1;
            nStruct

            .Namespace("Core30")
            .DefinedName("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.Default();

            temp.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();
            DomainManagment.Get("TempDomain").Dispose();
        }
Esempio n. 17
0
        public void Test2()
        {
            var domain   = DomainManagment.Create("SingleDomainAsmTest2");
            var assembly = domain.CreateAssembly("AsmTest1");

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


            var @class = assembly
                         .CreateClass("ClassAsm")
                         .Using("System")
                         .Public
                         .Inheritance("IAsmT1")
                         .Method(method => method
                                 .PublicMember
                                 .Name("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.Complier();
            var type   = assembly.GetType(@class.NamespaceScript + "." + "ClassAsm");

            domain.RemoveType(type);


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


            @class = assembly
                     .CreateClass("ClassAsm")
                     .Using("System")
                     .Public
                     .Inheritance("IAsmT1")
                     .Method(method => method
                             .PublicMember
                             .Name("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.Complier();
            type   = assembly.GetType(@class.NamespaceScript + ".ClassAsm");



            var builder = FastMethodOperator.Default();

            builder.Complier.Domain = domain;
            var @delegate = builder.Using(type).MethodBody(@"
ClassAsm obj = new ClassAsm();
return obj.ShowMethod(""Hello"");
").Complie <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(DomainManagment.IsDeleted("SingleDomainAsmTest1"));
#endif
        }