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

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

                                .Compile <Func <string> >();
                result = action();
                domain.Dispose();
            }
            return(result);
        }
Exemple #2
0
 public static Delegate Creator(Type type)
 {
     return(FastMethodOperator
            .Use(type.GetDomain())
            .Body($"return new {type.GetDevelopName()}();")
            .Return(type)
            .Compile());
 }
Exemple #3
0
        public void TestHelloWorld()
        {
            using (DomainManagement.CreateAndLock("MyDomain"))
            {
                var domain   = DomainManagement.CurrentDomain;
                var assembly = domain.CreateAssembly("MyAssembly");


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


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

                var result = assembly.GetAssembly();
                var type   = assembly.GetTypeFromShortName("TestClass");

                //单独创建一个程序集​方法
                var func = FastMethodOperator.Use(domain)
                           .Using(type)
                           .Body(@"
TestClass obj = new TestClass​();
return obj.ShowMethod(arg);")
                           .Compile <Func <string, string> >();
                Assert.Equal("Hello World!", func("Hello"));
            }
        }
Exemple #4
0
        public string GetResult2()
        {
            string result;
            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Lib", "Static", "ClassLibrary5.dll");

            Assert.True(File.Exists(path));
            using (DomainManagement.CreateAndLock("TempDomain12"))
            {
                var domain    = DomainManagement.CurrentDomain;
                var assemebly = domain.LoadPluginFromStream(path);
                var action    = FastMethodOperator.Use(domain)
                                .Using(assemebly)
                                .Body(@"Test.Instance.Name=""11""; return Test.Instance.Name;")
                                .Compile <Func <string> >();
                result = action();
                domain.Dispose();
                domain.Unload();
            }
            return(result);
        }
Exemple #5
0
        public string GetResult3()
        {
            string result;
            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Lib", "Json", "ClassLibrary6.dll");

            Assert.True(File.Exists(path));
            using (DomainManagement.CreateAndLock("TempDomain13"))
            {
                var domain    = DomainManagement.CurrentDomain;
                var assemebly = domain.LoadPluginFromStream(path);
                var action    = FastMethodOperator.Use(domain,
                                                       item => item.Compiler.ErrorBehavior = Natasha.Error.Model.ExceptionBehavior.Log
                                                       )
                                .Using(assemebly)
                                .Body(@"Class1 obj = new Class1(); return obj.Get();")
                                .Compile <Func <string> >();
                result = action();
                domain.Dispose();
                domain.Unload();
            }
            return(result);
        }