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.LoadPlugin(path); var action = FastMethodOperator.UseDomain(domain) .WithCS0104Handler() .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 Delegate TypeRouter(NBuildInfo info) { string methodBody; // 简单类型 // SimpleType if (info.ArrayBaseType.IsSimpleType()) { methodBody = GenerateSimpleTypeClone(info); } else if (info.ArrayBaseType == typeof(object)) { // 多维+复杂 // MultiD+Complex if (info.ArrayDimensions > 1) { methodBody = GenerateMultiDWithObjectClone(info); } // 1维+复杂 及 锯齿+复杂 // 1D+Complex and Jagged+Complex else { methodBody = GenerateJaggedWithObjectClone2(info); } } else { // 多维+复杂 // MultiD+Complex if (info.ArrayDimensions > 1) { methodBody = GenerateMultiDWithComplexClone(info); } // 1维+复杂 及 锯齿+复杂 // 1D+Complex and Jagged+Complex else { methodBody = GenerateJaggedWithComplexClone2(info); } } return(FastMethodOperator.UseDomain(info.CurrentType.GetDomain()) .Using("DeepClone") .Using(typeof(Array)) .Param(info.FatherType, "oldIns") .Body(methodBody) .Return(info.FatherType) .Compile()); }
public Delegate TypeRouter(NBuildInfo info) { if (info.CurrentType.IsGenericType) { StringBuilder scriptBuilder = new StringBuilder(); scriptBuilder.AppendLine(@"if(old!=default){ return "); // 初始化目标,区分实体类和接口 if (!info.CurrentType.IsInterface) { scriptBuilder.AppendLine($"new {info.CurrentTypeName}"); } scriptBuilder.AppendLine("(old.Select(item=>"); var parameters = info.CurrentType.GetGenericArguments(); if (parameters[0].IsSimpleType()) { scriptBuilder.Append("item"); } else if (parameters[0] == typeof(object)) { scriptBuilder.Append("ObjectCloneOperator.Clone(item)"); } else { scriptBuilder.Append("CloneOperator.Clone(item)"); } scriptBuilder.Append("));"); scriptBuilder.AppendLine(@"}return default;"); var action = FastMethodOperator.UseDomain(info.CurrentType.GetDomain()) .Using("DeepClone") .Using("System.Linq") .Param(info.FatherType, "old") .Body(scriptBuilder.ToString()) .Return(info.FatherType) .Compile(); return(action); } else { throw new Exception("暂不支持"); } }
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.LoadPlugin(path); var action = FastMethodOperator.UseDomain(domain) .Body(@"Test.Instance.Name=""11""; return Test.Instance.Name;") .Compile <Func <string> >(); result = action(); domain.Dispose(); domain.Unload(); } return(result); }
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.UseDomain(domain) .Using(type) .Body(@" TestClass obj = new TestClass(); return obj.ShowMethod(arg);") .Compile <Func <string, string> >(); Assert.Equal("Hello World!", func("Hello")); } }
public Delegate TypeRouter(NBuildInfo info) { var instanceName = "oldSource"; info.FatherType = info.FatherType == typeof(object) ? info.CurrentType : info.FatherType; if (info.CurrentType != info.FatherType) { instanceName = "old"; } CtorHandler = new CtorTempalte(info.CurrentType, instanceName); StringBuilder scriptBuilder = new StringBuilder(); var memberBuilder = new StringBuilder(); var builder = FastMethodOperator.UseDomain(info.CurrentType.GetDomain()); //构造函数处理: 不存在public无参构造函数无法克隆; if (info.CurrentType.GetConstructor(new Type[0]) == null) { return(default);
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.LoadPlugin(path); var action = FastMethodOperator.UseDomain(domain, item => item.LogCompilerError() ) .Body(@"Class1 obj = new Class1(); return obj.Get();") .Compile <Func <string> >(); result = action(); domain.Dispose(); domain.Unload(); } return(result); }
public Delegate TypeRouter(NBuildInfo info) { StringBuilder scriptBuilder = new StringBuilder(); scriptBuilder.AppendLine(@"if(old!=default){ return "); //初始化目标,区分实体类和接口 if (!info.CurrentType.IsInterface) { scriptBuilder.AppendLine($"new {info.CurrentTypeName}"); } scriptBuilder.AppendLine("(old.Select(item=>KeyValuePair.Create("); //克隆Key var keyType = info.CurrentType.GetGenericArguments()[0]; if (keyType.IsSimpleType()) { scriptBuilder.Append($"item.Key,"); } else if (keyType == typeof(object)) { scriptBuilder.AppendLine($"ObjectCloneOperator.Clone(item.Key),"); } else { scriptBuilder.AppendLine($"CloneOperator.Clone(item.Key),"); } //克隆Value var valueType = info.CurrentType.GetGenericArguments()[1]; if (valueType.IsSimpleType()) { scriptBuilder.Append($"item.Value"); } else if (keyType == typeof(object)) { scriptBuilder.AppendLine($"ObjectCloneOperator.Clone(item.Value),"); } else { scriptBuilder.AppendLine($"CloneOperator.Clone(item.Value)"); } //补全括号,返回默认值。 scriptBuilder.AppendLine(")));}return default;"); return(FastMethodOperator.UseDomain(info.CurrentType.GetDomain()) .Using("DeepClone") .Using("System.Linq") .Using(typeof(IDictionary)) .Using(typeof(KeyValuePair <,>)) .Param(info.FatherType, "old") .Body(scriptBuilder.ToString()) .Return(info.FatherType) .Compile()); }