public void Test3() { #if !NETCOREAPP2_2 using (DomainManagment.CreateAndLock("Default2")) { var domain = DomainManagment.CurrentDomain; var assembly = domain.CreateAssembly("ClassLibrary1"); assembly.AddScript("using System;namespace ClassLibrary1{ public class Class1{public string name;}}"); var result2 = assembly.Complier(); var type2 = assembly.GetType("ClassLibrary1.Class1"); domain.RemoveAssembly(result2); var assembly1 = domain.CreateAssembly("AsmTest2"); assembly1.AddScript("using System;namespace ClassLibrary1{ public class Class1{public string name;}}"); var result1 = assembly1.Complier(); var type1 = assembly1.GetType("ClassLibrary1.Class1"); Assert.NotEqual(result1, result2); Assert.Equal(type1.Name, type2.Name); lock (obj) { var func = NDomain.Default().Func <object>("return new Class1();", "ClassLibrary1"); Assert.Equal(result1, func().GetType().Assembly); } } #endif }
public void Test1() { string path1 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "File", "TextFile1.txt"); string path2 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "File", "TestFileModel.cs"); NAssembly assembly = new NAssembly("AsmTestFile"); assembly.AddFile(path1); assembly.AddFile(path2); var result = assembly.Complier(); Assert.NotNull(result); var type = assembly.GetType("TestFileModel"); Assert.NotNull(type); Assert.Equal("TestFileModel", type.Name); var @delegate = NDomain.Random().Func <string>("return new TestFileModel().Name;", result); Assert.Equal("aaa", @delegate()); }
public void Test1() { #if !NETCOREAPP2_2 lock (obj) { using (DomainManagment.CreateAndLock("TestSame")) { var domain = DomainManagment.CurrentDomain; var assembly = domain.CreateAssembly("ababab"); assembly.AddScript("using System;namespace ClassLibrary1{ public class Class1{public string name;}}"); var result2 = assembly.Complier(); var type2 = assembly.GetType("ClassLibrary1.Class1"); string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Lib", "Repeate", "ClassLibrary1.dll"); var result1 = domain.LoadStream(path); var type1 = result1.GetTypes().First(item => item.Name == "Class1"); Assert.True(domain.RemoveDll(path)); Assert.Equal("TestSame", DomainManagment.CurrentDomain.Name); Assert.NotEqual(result1, result2); Assert.Equal(type1.Name, type2.Name); var func = NDomain.Default().Func <object>("return new Class1();", "ClassLibrary1"); Assert.Equal(result2, func().GetType().Assembly); } } #endif }
public static void RunDelegate2() { //------创建一个域(方便卸载)----//-----创建Func方法--------// var func = NDomain.Create("NDomain2").Func <string, string>("return arg;"); Assert.Equal("1", func("1")); }
public void Test3() { var func = NDomain.Random().Func <Cs0104Model1.String>("String a = new String(Cs0104Model.A); return a;", "Cs0104Model1"); var result = func(); Assert.Equal("Cs0104Model1", result.GetType().Namespace); }
public void Test2() { var func = NDomain.Random().Func <object>("CS0104GModel<int> a = new CS0104GModel<int>(); return a;", "Cs0104Model1"); var result = func(); Assert.Equal("Cs0104Model1", result.GetType().Namespace); }
public void TestDelegateEqual() { var domain = DomainManagment.Random; var action = NDomain.Create(domain).Action( @"int i = 1+1;"); Assert.Equal(domain, action.GetDomain()); }
public static void RunDelegate5() { NormalTestModel model = new NormalTestModel(); var func = NDomain.Create("NDomain5").Action <NormalTestModel, int>("arg1.Age=arg2;"); func(model, 1); Assert.Equal(1, model.Age); }
public static void RunDelegate6() { NormalTestModel model = new NormalTestModel(); var func = NDomain.Create("NDomain6").Action <NormalTestModel, int, int>("arg1.Age=arg2+arg3;"); func(model, 1, 2); Assert.Equal(3, model.Age); }
public void CustomerFindTree() { HashDelegate = NDomain.Random().UnsafeFunc <string, int>(BTFTemplate.GetCustomerBTFScript(ScriptDict, "arg.GetHashCode()", item => item.GetHashCode().ToString()) + "return default;"); foreach (var item in Dict) { Assert.Equal(item.Value, HashDelegate(item.Key)); } }
public void HashFindTree() { HashDelegate = NDomain.Random().UnsafeFunc <string, int>(BTFTemplate.GetHashBTFScript(ScriptDict) + "return default;"); foreach (var item in Dict) { Assert.Equal(item.Value, HashDelegate(item.Key)); } }
public static void RunDelegate4() { NormalTestModel model = new NormalTestModel(); var func = NDomain.Create("NDomain4").Action <NormalTestModel>("obj.Age=1;"); func(model); Assert.Equal(1, model.Age); }
public static void RunDelegate8() { var action = NDomain.Random().Delegate <TestDelegate>(@" return value.Length;"); int result = action("Hello"); Assert.Equal(5, result); }
public void Precache() { Type type = typeof(CallModel); DynamicMethod method = new DynamicMethod("GetString", typeof(string), new Type[] { type }); ILGenerator il = method.GetILGenerator(); il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldfld, type.GetField("Age")); il.Emit(OpCodes.Ret); EmitGetString = (Func <CallModel, string>)(method.CreateDelegate(typeof(Func <CallModel, string>))); method = new DynamicMethod("GetDateTime", typeof(DateTime), new Type[] { type }); il = method.GetILGenerator(); il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldfld, type.GetField("CreateTime")); il.Emit(OpCodes.Ret); EmitGetDateTime = (Func <CallModel, DateTime>)(method.CreateDelegate(typeof(Func <CallModel, DateTime>))); method = new DynamicMethod("SetDateString", null, new Type[] { type, typeof(string) }); il = method.GetILGenerator(); il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldarg_1); il.Emit(OpCodes.Stfld, type.GetField("Age")); il.Emit(OpCodes.Ret); EmitSetString = (Action <CallModel, string>)(method.CreateDelegate(typeof(Action <CallModel, string>))); method = new DynamicMethod("SetDateTime", null, new Type[] { type, typeof(DateTime) }); il = method.GetILGenerator(); il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldarg_1); il.Emit(OpCodes.Stfld, type.GetField("CreateTime")); il.Emit(OpCodes.Ret); EmitSetDateTime = (Action <CallModel, DateTime>)(method.CreateDelegate(typeof(Action <CallModel, DateTime>))); NatashaGetString = NDomain.Default().Func <CallModel, string>("return arg.Age;"); NatashaGetString(OriginModel); OriginGetString = item => item.Age; NatashaGetDateTime = NDomain.Default().Func <CallModel, DateTime>("return arg.CreateTime;"); NatashaGetDateTime(OriginModel); OriginGetDateTime = item => item.CreateTime; NatashaSetString = NDomain.Default().Action <CallModel, string>("arg1.Age=arg2;"); NatashaSetString(OriginModel, OriginModel.Age); OriginSetString = (item, value) => item.Age = value; NatashaSetDateTime = DelegateOperator <ValueDelegate> .Delegate("model.CreateTime=value;"); NatashaSetDateTime(OriginModel, OriginModel.CreateTime); OriginSetDateTime = OriginDateTime; }
public static int RunDelegate6() { NormalTestModel model = new NormalTestModel(); var func = NDomain.Create("NDomain6").Action <NormalTestModel, int, int>("arg1.Age=arg2+arg3;"); func(model, 1, 2); func.DisposeDomain(); return(model.Age); }
public void GroupsPrecisionFindTree() { PrecisionDelegate = NDomain.Random().UnsafeFunc <string, int>(BTFTemplate.GetGroupPrecisionPointBTFScript(ScriptDict) + "return default;"); //var temp = BTFTemplate.GetPrecisionPointBTFScript(ScriptDict) + "return default;"; foreach (var item in Dict) { Assert.Equal(item.Value, PrecisionDelegate(item.Key)); } }
public void TestType1() { var type = NDomain.Create("NDomain8").GetType( @"public class DomainTest1{ public string Name; public DomainOperator Operator; }"); Assert.Equal("DomainTest1", type.Name); }
public void TestTypeEqual() { var domain = DomainManagment.Random; var type = NDomain.Create(domain).GetType( @"public class DomainTest1{ public string Name; public DomainOperator Operator; }"); Assert.Equal(domain, type.GetDomain()); }
public static async void RunAsyncDelegate6() { var action = NDomain.Random().UnsafeAsyncFunc <string, string, Task <string> >(@" string result = arg1 +"" ""+ arg2; Console.WriteLine(result); return result;"); string result = await action("Hello", "World1!"); Assert.Equal("Hello World1!", result); }
public static void ShowPlugin(string path) { //使用随机域 var domain = DomainManagment.Random; //如果是2.0 需要单独添加引用,如果是3.0 就可以注释掉了 //domain.LoadStream(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "lib", "core2", "MySql.Data.dll")); //加载插件 var assembly = domain.LoadStream(path); //撸代码 var action = NDomain.Create(domain).Action("Class2 temp = new Class2();Console.WriteLine(temp.Get());", assembly); //执行 action(); //卸载 action.DisposeDomain(); }
static void Main(string[] args) { /* * 在此之前,你需要右键,选择工程文件,在你的.csproj里面 * * 写上这样一句浪漫的话: * * <PreserveCompilationContext>true</PreserveCompilationContext> */ string text = @"namespace HelloWorld { public class Test { public Test(){ Name=""111""; } public string Name; public int Age{get;set;} } }"; //根据脚本创建动态类 AssemblyComplier oop = new AssemblyComplier("test"); oop.Add(text); Type type = oop.GetType("Test"); Console.WriteLine(type.Name); var action = NDomain.Random().Action(""); var a = action.Method; Console.WriteLine(action.Method.Module.Assembly); Console.ReadKey(); }
static void Main(string[] args) { var domain = DomainManagment.Random; var type = NDomain.Create(domain).GetType("public class A{ public A(){Name=\"1\"; }public string Name;}"); Console.WriteLine(type.FullName); var func = NDomain.Create(domain).Func <string>("return (new A()).Name;"); Console.WriteLine(func()); type.RemoveReferences(); type = NDomain.Create(domain).GetType("public class A{ public A(){Name=\"2\"; }public string Name;}"); func = NDomain.Create(domain).Func <string>("return (new A()).Name;"); Console.WriteLine(type.FullName); Console.WriteLine(func()); domain = DomainManagment.Create("a"); using (DomainManagment.Lock("a")) { Console.WriteLine(domain == (AssemblyDomain)AssemblyLoadContext.CurrentContextualReflectionContext); } }
public static Func <object, object> Create(Type type) { return(NDomain.Create(type.GetDomain()).Func <object, object>($"return CloneOperator.Clone(({type.GetDevelopName()})arg);", type, "DeepClone")); }
public static void RunDelegate1() { var func = NDomain.Create("NDomain1").Func <string>("return \"1\";"); Assert.Equal("1", func()); }
public static void RunDelegate7() { var func = NDomain.Create("NDomain7").Func <string>("return OtherNameSpaceMethod.FromDate(DateTime.Now);"); Assert.Equal(DateTime.Now.ToString("yyyy-MM"), func()); }
public static void RunDelegate3() { var func = NDomain.Create("NDomain3").Func <string, string, string>("return arg1+arg2;"); Assert.Equal("12", func("1", "2")); }