public string GetResult1() { string result; string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Lib", "Sql", "ClassLibrary1.dll"); Console.WriteLine(path); Assert.True(File.Exists(path)); using (DomainManagment.CreateAndLock("TempDomain11")) { var domain = DomainManagment.CurrentDomain; var assemebly = domain.LoadFile(path); var action = FastMethodOperator.Create() .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); }
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); } } var action = FastMethodOperator.Create(info.CurrentType.GetDomain()) .Using("DeepClone") .Using(typeof(Array)) .Param(info.FatherType, "oldIns") .MethodBody(methodBody) .Return(info.FatherType) .Complie(); return(action); }
public static void RunInnerDelegate() { var delegateAction = FastMethodOperator .Create() .MethodBody(@" OopTestModel.InnerClass a = new OopTestModel.InnerClass(); a.Name =""abc""; return a;") .Return <OopTestModel.InnerClass>() .Complie(); var result = ((Func <OopTestModel.InnerClass>)delegateAction)?.Invoke(); Assert.Equal("abc", result.Name); }
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("ClassAsm"); var builder = FastMethodOperator.Create(); 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")); }
public static void RunDelegate2() { var delegateAction = FastMethodOperator .Create() .Param <string>("str1") .Param <string>("str2") .MethodBody(@" string result = str1 +"" ""+ str2; Console.WriteLine(result); return result;") .Return <string>() .Complie <Func <string, string, string> >(); string result = delegateAction?.Invoke("Hello", "World2!"); Assert.Equal("Hello World2!", result); }
public void Test3() { var builder = FastMethodOperator.Create(); var delegateAction = builder .Param <string>("str1") .Param <string>("str2") .MethodBody(@" string result = str1 +"" ""+ str2; Console.WriteLine(result)1; return result;") .Return <string>() .Complie(); Assert.Null(delegateAction); Assert.Equal(ComplieError.Syntax, builder.Complier.ComplieException.ErrorFlag); Assert.Equal(ComplieError.Syntax, builder.Complier.SyntaxExceptions[0].ErrorFlag); }
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.Create(info.CurrentType.GetDomain()) .Using("DeepClone") .Using("System.Linq") .Param(info.FatherType, "old") .MethodBody(scriptBuilder.ToString()) .Return(info.FatherType) .Complie(); return(action); } else { throw new Exception("暂不支持"); } }
public static async void RunAsyncDelegate4() { var delegateAction = FastMethodOperator.Create() .UseAsync() .MethodBody(@" await Task.Delay(100); string result = arg1 +"" ""+ arg2; Console.WriteLine(result); return result;") .Complie <Func <string, string, Task <string> > >(); string result = await delegateAction?.Invoke("Hello", "World2!"); Assert.Equal("Hello World2!", result); }
public static async void RunAsyncDelegate1() { var delegateAction = FastMethodOperator .Create() .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); }
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);"); //创建一个类并实现接口 assembly .CreateClass("TestClass") .Using("System") .Public .Inheritance("InterfaceTest") .Method(method => method .PublicMember .Name("ShowMethod") .Param <string>("str") .Body("return str+\" World!\";") .Return <string>()); var result = assembly.Complier(); var type = assembly.GetType("TestClass"); //单独创建一个程序集方法 var func = FastMethodOperator.Create() .Using(type) .MethodBody(@" TestClass obj = new TestClass(); return obj.ShowMethod(arg);") .Complie <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 (DomainManagment.CreateAndLock("TempDomain12")) { var domain = DomainManagment.CurrentDomain; var assemebly = domain.LoadFile(path); var action = FastMethodOperator.Create() .Using(assemebly) .MethodBody(@"Test.Instance.Name=""11""; return Test.Instance.Name;") .Complie <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 (DomainManagment.CreateAndLock("TempDomain13")) { var domain = DomainManagment.CurrentDomain; var assemebly = domain.LoadStream(path); var action = FastMethodOperator.Create() .Using(assemebly) .MethodBody(@"Class1 obj = new Class1(); return obj.Get();") .Complie <Func <string> >(); result = action(); domain.Dispose(); domain.Unload(); } return(result); }
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.Create(info.CurrentType.GetDomain()); //构造函数处理: 不存在public无参构造函数无法克隆; if (info.CurrentType.GetConstructor(new Type[0]) == null) { return(default);
public static void Test() { for (int i = 0; i < count; i++) { var ad = DomainManagment.Create("test" + i.ToString()); var builder = FastMethodOperator.Create(); builder.Complier.Domain = ad; func[i] = builder.MethodBody($@" 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; ").Complie <Func <int[]> >(); func[i](); } }
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;"); var action = FastMethodOperator.Create(info.CurrentType.GetDomain()) .Using("DeepClone") .Using("System.Linq") .Using(typeof(IDictionary)) .Using(typeof(KeyValuePair <,>)) .Param(info.FatherType, "old") .MethodBody(scriptBuilder.ToString()) .Return(info.FatherType) .Complie(); return(action); }
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("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("ClassAsm"); var builder = FastMethodOperator.Create(); 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 }
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.Complier.Domain = domain1; nStruct .Namespace("Core30") .OopName("Test") .Ctor(builder => builder .PublicMember .Param <string>("name") .Body(@"Name=name+""3"";")) .PublicField <string>("Name"); var type4 = nStruct.GetType(); //nStruct = new NStruct(); //nStruct // .InDomain(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.Create(); temp.Complier.Domain = domain1; temp //.Using<Test>() //.Using(type) .Using(type4) //.MethodAttribute<MethodImplAttribute>("MethodImplOptions.NoInlining") .MethodBody(@" Test obj = new Test(""Hello World!""); Console.WriteLine(obj.Name);" ); action = temp.Complie <Action>(); action(); DomainManagment.Get("TempDomain").Dispose(); }
static void Main(string[] args) { // NAssembly nAssembly = new NAssembly("a"); //// Show(); //// Console.WriteLine(AssemblyManagment.Count("TempDomain")); // NStruct nStruct1 = new NStruct(); // nStruct1 // .Namespace("Core30") // .OopName("Test") // .Ctor(builder => builder // .PublicMember // .Param<string>("name") // .Body("Name=name;")) // .PublicField<string>("Name"); // var typ1e = nStruct1.GetType(); // Console.WriteLine(AssemblyManagment.Count("Default")); // var domain2 = AssemblyManagment.Create("TempDomain2"); // using (AssemblyManagment.Lock("TempDomain2")) // { // //do sth // 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(); // nStruct = new NStruct(); // nStruct // .Namespace("Core30") // .OopName("Test1") // .Ctor(builder => builder // .PublicMember // .Param<string>("name") // .Body("Name=name;")) // .PublicField<string>("Name"); // type = nStruct.GetType(); // } // Console.WriteLine(AssemblyManagment.Count("TempDomain2")); // using (AssemblyManagment.CreateAndLock("TempDomain3")) // { // //do sth // 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(); // } // Console.WriteLine(AssemblyManagment.Count("TempDomain3")); //ShowQ(); //Thread.Sleep(2000); //Testqq(); //Thread.Sleep(2000); //TestMemoery2(); //Testt(); string result; string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Lib", "Sql", "ClassLibrary1.dll"); using (DomainManagment.CreateAndLock("TempDomain11")) { var domain = DomainManagment.CurrentDomain; var assemebly = domain.LoadStream(path); var action = FastMethodOperator.Create() .Using(assemebly) .MethodBody(@" try{ //MySqlConnection conn = new MySqlConnection(""""); //conn.Open(); Class1 a = new Class1(); return a.Show(); } catch(Exception e){ return e.Message; } return default;").Return <string>() .Complie <Func <string> >(); result = action(); Console.WriteLine(result); } Console.ReadKey(); }
static void Main(string[] args) { if (args == null) { throw new ArgumentNullException(nameof(args)); } var action = FastMethodOperator.Create() .MethodBody("return 1+1;") .Return <int>() .Complie <Func <int> >(); action(); FakeMethodOperator.Create() .UseMethod <TestB>("TestMethod") .StaticMethodContent($@"Console.WriteLine(""Hello World!"");") .Complie <Action>(); FakeMethodOperator.Create() .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.Create() .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(); }