Exemple #1
0
        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());
        }
Exemple #3
0
        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
        }
Exemple #4
0
        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
        }
Exemple #5
0
 public static Func <object, object> Create(Type type)
 {
     return(NFunc <object, object> .Delegate($"return CloneOperator.Clone(({type.GetDevelopName()})arg);", type, "DeepClone"));
 }