简体   繁体   English

如何从另一个appDomain调用类的方法

[英]how to call a method of a class from another appDomain

my application want to call a method of a class that is from another AppDomain. 我的应用程序想要调用另一个AppDomain中的类的方法。

       AppDomain env = AppDomain.CreateDomain(
            "test",
            null,
            new AppDomainSetup() { ApplicationName = "test" }
            );

        Assembly a = Assembly.LoadFrom("d:\\testenv1\\test2.dll");
        //env.AssemblyResolve += new ResolveEventHandler(env_AssemblyResolve);
        env.Load(a.FullName);

        ObjectHandle o = env.CreateInstance(a.FullName, "Test2.Class1");

now i have the object handle of the Test2.Class1, but i have no idea how to invode the "action" method of the Class1 class. 现在我有Test2.Class1的对象句柄,但我不知道如何调用Class1类的“操作”方法。

the "action" method likes this: “操作”方法如下所示:

    public void action()
    {
        Console.WriteLine(AppDomain.CurrentDomain.FriendlyName + " ok");
    }

i tried to use o.unwrap() method to get the reference of the object, but it seems the object has been transferred into the current domain, so the output of the "action" method prints the current domain name. 我尝试使用o.unwrap()方法获取对象的引用,但似乎对象已转移到当前域中,因此“ action”方法的输出将打印当前域名。

将要用于跨应用程序域通信的对象标记为MarshalByRefObject

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM