Esempio n. 1
0
        public static void RunDelegate2()
        {
            //------创建一个域(方便卸载)----//-----创建Func方法--------//
            var func = NDomain.Create("NDomain2").Func <string, string>("return arg;");

            Assert.Equal("1", func("1"));
        }
Esempio n. 2
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 = NDomain.Create().Func <object>("return new Class1();", "ClassLibrary1");
                    Assert.Equal(result2, func().GetType().Assembly);
                }
            }
#endif
        }
Esempio n. 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 = NDomain.Create().Func <object>("return new Class1();", "ClassLibrary1");
                    Assert.Equal(result1, func().GetType().Assembly);
                }
            }
#endif
        }
Esempio n. 4
0
        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);
        }
Esempio n. 5
0
        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);
        }
Esempio n. 6
0
        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);
        }
Esempio n. 7
0
        public void TestDelegateEqual()
        {
            var domain = DomainManagment.Random;
            var action = NDomain.Create(domain).Action(
                @"int i = 1+1;");

            Assert.Equal(domain, action.GetDomain());
        }
Esempio n. 8
0
        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);
        }
Esempio n. 9
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 = NDomain.Create().Func <CallModel, string>("return arg.Age;");
            NatashaGetString(OriginModel);
            OriginGetString = item => item.Age;


            NatashaGetDateTime = NDomain.Create().Func <CallModel, DateTime>("return arg.CreateTime;");
            NatashaGetDateTime(OriginModel);
            OriginGetDateTime = item => item.CreateTime;


            NatashaSetString = NDomain.Create().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;
        }
Esempio n. 10
0
        public void TestType1()
        {
            var type = NDomain.Create("NDomain8").GetType(
                @"public class  DomainTest1{
                        public string Name;
                        public DomainOperator Operator;
                }");

            Assert.Equal("DomainTest1", type.Name);
        }
Esempio n. 11
0
        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());
        }
Esempio n. 12
0
        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();
        }
Esempio n. 13
0
        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);
            }
        }
Esempio n. 14
0
        public static void RunDelegate7()
        {
            var func = NDomain.Create("NDomain7").Func <string>("return OtherNameSpaceMethod.FromDate(DateTime.Now);");

            Assert.Equal(DateTime.Now.ToString("yyyy-MM"), func());
        }
Esempio n. 15
0
        public static void RunDelegate3()
        {
            var func = NDomain.Create("NDomain3").Func <string, string, string>("return arg1+arg2;");

            Assert.Equal("12", func("1", "2"));
        }
Esempio n. 16
0
 public static Func <object, object> Create(Type type)
 {
     return(NDomain.Create(type.GetDomain()).Func <object, object>($"return CloneOperator.Clone(({type.GetDevelopName()})arg);", type, "DeepClone"));
 }
Esempio n. 17
0
        public static void RunDelegate1()
        {
            var func = NDomain.Create("NDomain1").Func <string>("return \"1\";");

            Assert.Equal("1", func());
        }