static void Main(string[] args) { AssemblyDomain.Init(); var hwFunc = FastMethodOperator .RandomDomain() .Param(typeof(string), "str1") .Param <string>("str2") .Body("return str1+str2;") .Return <string>() .Compile <Func <string, string, string> >(); Console.WriteLine(hwFunc("Hello", " World!")); var a123 = NClass.UseDomain(typeof(Program).GetDomain()); var domain = DomainManagement.Random; var type = NDelegate.UseDomain(domain).GetType("public class A{ public A(){Name=\"1\"; }public string Name;}"); var func = NDelegate.UseDomain(domain).Func <string>("return (new A()).Name;"); Console.WriteLine(type.FullName); Console.WriteLine(func()); //type.RemoveReferences(); type = NDelegate.UseDomain(domain).GetType("public class A{ public A(){Name=\"2\"; }public string Name;}"); func = NDelegate.UseDomain(domain).Func <string>("return (new A()).Name;"); Console.WriteLine(type.FullName); Console.WriteLine(func()); domain = DomainManagement.Create("a"); using (DomainManagement.Lock("a")) { Console.WriteLine(domain == (AssemblyDomain)AssemblyLoadContext.CurrentContextualReflectionContext); } }
public void RunDelegate3() { var delegateAction = FastMethodOperator.RandomDomain() .Body(@"var temp = obj;") .Compile <Action <string> >(); delegateAction?.Invoke("Hello"); Assert.NotNull(delegateAction); }
public void RunDelegate2() { var delegateAction = FastMethodOperator.RandomDomain() .Body(@"return arg1 +"" ""+ arg2;") .Compile <Func <string, string, string> >(); string result = delegateAction?.Invoke("Hello", "World2!"); Assert.Equal("Hello World2!", result); }
public void RunDelegate1() { var delegateAction = FastMethodOperator .RandomDomain() .Param <string>("str1") .Param <string>("str2") .Body(@" string result = str1 +"" ""+ str2; return result;") .Return <string>() .Compile <Func <string, string, string> >(); string result = delegateAction?.Invoke("Hello", "World2!"); Assert.Equal("Hello World2!", result); }
private Func <TIn, TOut> GetFunc <TIn, TOut>(string argsName = "arguments") { return(FastMethodOperator.RandomDomain(o => { o.ThrowAndLogCompilerError(); o.ThrowAndLogSyntaxError(); o.UseStreamCompile(); foreach (var ns in _usingList) { o.Add($"using {ns};"); } }).Param <TIn>(argsName) .Body(_body) .Return <TOut>() .Compile <Func <TIn, TOut> >()); }