public void Test3() { #if !NETCOREAPP2_2 using (DomainManagment.CreateAndLock("Default2")) { var domain = DomainManagment.CurrentDomain; var assembly = domain.CreateAssembly("AsmTest1"); assembly.AddScript("using System;namespace ClassLibrary1{ public class Class1{public string name;}}"); var result2 = assembly.Complier(); var type2 = assembly.GetType("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("Class1"); Assert.NotEqual(result1, result2); Assert.Equal(type1.Name, type2.Name); lock (obj) { var func = NFunc <object> .Delegate("return new Class1();", "ClassLibrary1"); Assert.Equal(result1, func().GetType().Assembly); } } #endif }
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 = NFunc <CallModel, string> .Delegate("return arg.Age;"); NatashaGetString(OriginModel); OriginGetString = item => item.Age; NatashaGetDateTime = NFunc <CallModel, DateTime> .Delegate("return arg.CreateTime;"); NatashaGetDateTime(OriginModel); OriginGetDateTime = item => item.CreateTime; NatashaSetString = NAction <CallModel, string> .Delegate("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 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 = NFunc <string> .Delegate("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("AsmTest1"); assembly.AddScript("using System;namespace ClassLibrary1{ public class Class1{public string name;}}"); var result2 = assembly.Complier(); var type2 = assembly.GetType("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 = NFunc <object> .Delegate("return new Class1();", "ClassLibrary1"); Assert.Equal(result2, func().GetType().Assembly); } } #endif }
public void FuzzyFindTree() { FuzzyDelegate = NFunc<string, int>.UnsafeDelegate(BTFTemplate.GetFuzzyPointBTFScript(ScriptDict) + "return default;"); foreach (var item in Dict) { Assert.Equal(item.Value, FuzzyDelegate(item.Key)); } }
public static async void RunAsyncDelegate6() { var action = NFunc <string, string, Task <string> > .UnsafeAsyncDelegate(@" string result = arg1 +"" ""+ arg2; Console.WriteLine(result); return result;"); string result = await action("Hello", "World1!"); Assert.Equal("Hello World1!", result); }
} // map() public static List <O> map <I, SuperI, O> (IEnumerable <I> items, NFunc <SuperI, O> func) where I : SuperI { List <O> result = new List <O>(); foreach (I item in items) { O output = func.eval((SuperI)item); result.Add(output); } return(result); } // map()
public static void RunDelegate5() { var action = NFunc <string, string, string> .UnsafeDelegate(@" string result = arg1 +"" ""+ arg2; Console.WriteLine(result); return result;"); string result = action("Hello", "World1!"); Assert.Equal("Hello World1!", result); }
} // read() public E read <E>(NFunc <NInputStream, E> readCallback) { NInputStream in_stream = read(); try { E result = readCallback.eval(in_stream); return(result); } catch (Exception e) { throw new NResourceException("Error occurred in read callback", e); } finally { FileHelper.safeClose(in_stream); } } // read()
} // filter() public static List <O> map <I, SuperI, O> (I[] items, NFunc <SuperI, O> func) where I : SuperI { return(map(NArrays.AsList(items), func)); } // map()
/// <summary> /// 値を返すタイプのコマンドを登録します. /// </summary> /// <param name="name">Name.</param> /// <param name="func">Func.</param> public NovelRuntime RegisterCommand(string name, NFunc func) { commands[name] = func; return(this); }
public static Func <object, object> Create(Type type) { return(NFunc <object, object> .Delegate($"return CloneOperator.Clone(({type.GetDevelopName()})arg);", type, "DeepClone")); }