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); }
public static Delegate Creator(Type type) { return(FastMethodOperator .Use(type.GetDomain()) .Body($"return new {type.GetDevelopName()}();") .Return(type) .Compile()); }
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")); } }
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); }
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); }