Exemple #1
0
        public static void Show1()
        {
            string result;
            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Lib", "Sql", "ClassLibrary1.dll");

            using (DomainManagement.CreateAndLock("TempDomain11"))
            {
                var domain    = DomainManagement.CurrentDomain;
                var assemebly = domain.LoadPluginFromStream(path);
                var action    = FastMethodOperator.DefaultDomain()
                                .Using(assemebly)
                                .Body(@"
try{
Class1 a = new Class1();
return  a.Show();
}
catch(Exception e){
    return e.Message;
}
return default;").Return <string>()

                                .Compile <Func <string> >();
                result = action();
                Console.WriteLine(result);
            }
        }
Exemple #2
0
        public void Test2()
        {
#if !NETCOREAPP2_2
            using (DomainManagement.CreateAndLock("Default1"))
            {
                var domain   = DomainManagement.CurrentDomain;
                var assembly = domain.CreateAssembly("ClassLibrary1");
                assembly.AddScript("using System;namespace ClassLibrary1{ public class Class1{public string name;}}");
                var result2 = assembly.GetAssembly();
                var type2   = result2.GetTypes().First(item => item.Name == "Class1");

                try
                {
                    var assembly1 = domain.CreateAssembly("AsmTest2");
                    assembly1.AddScript("using System;namespace ClassLibrary1{ public class Class1{public string name;}}");
                    var result1 = assembly1.GetAssembly();
                    var type1   = result1.GetTypes().First(item => item.Name == "Class1");

                    Assert.NotEqual(result1, result2);
                    Assert.Equal(type1.Name, type2.Name);
                    lock (obj)
                    {
                        var func = NDelegate.UseDomain(domain)
                                   .AddUsing("ClassLibrary1")
                                   .Func <object>("return new Class1();");
                        Assert.Equal(result2, func().GetType().Assembly);
                    }
                }
                catch (Exception ex)
                {
                    Assert.NotNull(ex);
                }
            }
#endif
        }
Exemple #3
0
        public void Test3()
        {
#if !NETCOREAPP2_2
            NSucceedLog.Enabled = true;
            using (DomainManagement.CreateAndLock("Default2"))
            {
                var domain   = DomainManagement.CurrentDomain;
                var assembly = domain.CreateAssembly("ClassLibrary1");
                assembly.AddScript("using System;namespace ClassLibrary1{ public class Class1{public string name;}}");
                var result2 = assembly.GetAssembly();
                var type2   = result2.GetTypes().First(item => item.Name == "Class1");
                //domain.RemoveAssembly(result2);


                var assembly1 = domain.CreateAssembly("AsmTest22");
                assembly1.AddScript("using System;namespace ClassLibrary1{ public class Class1{public string name;}}");
                var result1 = assembly1.GetAssembly();
                var type1   = result1.GetTypes().First(item => item.Name == "Class1");


                Assert.NotEqual(result1, result2);
                Assert.Equal(type1.Name, type2.Name);
                domain.Remove(result2);

                lock (obj)
                {
                    //Class1 同时存在于 ClassLibrary1 和 AsmTest22 中
                    var func = NDelegate.UseDomain(domain).AddUsing("ClassLibrary1").Func <object>("return new Class1();");
                    Assert.Equal(result1, func().GetType().Assembly);
                }
            }
#endif
        }
Exemple #4
0
        public void Test1()
        {
#if !NETCOREAPP2_2
            lock (obj)
            {
                using (DomainManagement.CreateAndLock("TestSame"))
                {
                    var domain   = DomainManagement.CurrentDomain;
                    var assembly = domain.CreateAssembly("ababab");
                    assembly.AddScript("using System;namespace ClassLibrary1{ public class Class1{public string name;}}");
                    var result2 = assembly.GetAssembly();
                    var type2   = result2.GetTypes().First(item => item.Name == "Class1");


                    string path    = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Lib", "Repeate", "ClassLibrary1.dll");
                    var    result1 = domain.LoadPluginFromStream(path);
                    var    type1   = result1.GetTypes().First(item => item.Name == "Class1");
                    domain.Remove(path);


                    Assert.Equal("TestSame", DomainManagement.CurrentDomain.Name);
                    Assert.NotEqual(result1, result2);
                    Assert.Equal(type1.Name, type2.Name);


                    var func = NDelegate.UseDomain(domain).AddUsing("ClassLibrary1").Func <object>("return new Class1();");
                    Assert.Equal(result2, func().GetType().Assembly);
                }
            }
#endif
        }
        public string GetResult1()
        {
            string result;
            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Lib", "Sql", "ClassLibrary1.dll");

            Assert.True(File.Exists(path));
            using (DomainManagement.CreateAndLock("TempDADomain11"))
            {
                var domain    = DomainManagement.CurrentDomain;
                var assemebly = domain.LoadPlugin(path);
                var action    = FastMethodOperator.UseDomain(domain)
                                .WithCS0104Handler()
                                .Body(@"
try{
Class1 a = new Class1();
return  a.Show();
}
catch(Exception e){
    Console.WriteLine(e.Message);  
    return e.Message;
}
return default;").Return <string>()

                                .Compile <Func <string> >();
                result = action();
                domain.Dispose();
            }
            return(result);
        }
Exemple #6
0
 public void Test3()
 {
     using (DomainManagement.CreateAndLock("CDomain1"))
     {
         var domain = DomainManagement.CurrentDomain;
         Assert.Equal("CDomain1", domain.Name);
     }
 }
Exemple #7
0
 public void Test4()
 {
     using (DomainManagement.CreateAndLock("TempDomain14"))
     {
         var(Assembly, TypeCache) = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Lib", "Sql", "ClassLibrary1.dll");
         Assert.Equal("ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", Assembly.FullName);
         Assert.Equal("Class1", TypeCache["ClassLibrary1.Class1"].Name);
         Assert.Equal("ClassLibrary1.Class1", TypeCache["ClassLibrary1.Class1"].FullName);
     }
 }
Exemple #8
0
        public void Test4()
        {
            using (DomainManagement.CreateAndLock("TempDomain15"))
            {
                var(Assembly, Types, Exception) = @"
using System;
namespace TT{

public class Test{


}

}
";
                Assert.Equal("Test", Types[0].Name);
            }
        }
        public string GetResult2()
        {
            string result;
            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Lib", "Static", "ClassLibrary5.dll");

            Assert.True(File.Exists(path));
            using (DomainManagement.CreateAndLock("TempDomain12"))
            {
                var domain    = DomainManagement.CurrentDomain;
                var assemebly = domain.LoadPlugin(path);
                var action    = FastMethodOperator.UseDomain(domain)
                                .Body(@"Test.Instance.Name=""11""; return Test.Instance.Name;")
                                .Compile <Func <string> >();
                result = action();
                domain.Dispose();
                domain.Unload();
            }
            return(result);
        }
Exemple #10
0
        public void TestHelloWorld()
        {
            using (DomainManagement.CreateAndLock("MyDomain"))
            {
                var domain   = DomainManagement.CurrentDomain;
                var assembly = domain.CreateAssembly("MyAssembly");


                //创建一个接口
                assembly
                .CreateInterface("InterfaceTest")
                .Using("System")
                .Public()
                .Body("string ShowMethod(string str);");


                //创建一个类并实现​接口
                var nameSpace = assembly
                                .CreateClass("TestClass​")
                                .Using("System")
                                .Public()
                                .Inheritance("InterfaceTest")
                                .Method(method => method
                                        .Public()
                                        .DefinedName("ShowMethod")
                                        .Param <string>("str")
                                        .Body("return str+\" World!\";")
                                        .Return <string>()).NamespaceScript;

                var result = assembly.GetAssembly();
                var type   = assembly.GetTypeFromShortName("TestClass");

                //单独创建一个程序集​方法
                var func = FastMethodOperator.UseDomain(domain)
                           .Using(type)
                           .Body(@"
TestClass obj = new TestClass​();
return obj.ShowMethod(arg);")
                           .Compile <Func <string, string> >();
                Assert.Equal("Hello World!", func("Hello"));
            }
        }
        public string GetResult3()
        {
            string result;
            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Lib", "Json", "ClassLibrary6.dll");

            Assert.True(File.Exists(path));
            using (DomainManagement.CreateAndLock("TempDomain13"))
            {
                var domain    = DomainManagement.CurrentDomain;
                var assemebly = domain.LoadPlugin(path);
                var action    = FastMethodOperator.UseDomain(domain,
                                                             item => item.LogCompilerError()
                                                             )
                                .Body(@"Class1 obj = new Class1(); return obj.Get();")
                                .Compile <Func <string> >();
                result = action();
                domain.Dispose();
                domain.Unload();
            }
            return(result);
        }