如何从另一个应用程序域调用类的方法

编程入门 行业动态 更新时间:2024-10-27 02:22:53
本文介绍了如何从另一个应用程序域调用类的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我的应用程序想要调用来自另一个 AppDomain 的类的方法.

my application want to call a method of a class that is from another 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");

现在我有了 Test2.Class1 的对象句柄,但我不知道如何调用 Class1 类的action"方法.

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.

action"方法是这样的:

the "action" method likes this:

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

我尝试使用o.unwrap()方法来获取对象的引用,但是好像对象已经转移到了当前域中,所以action"方法的输出打印了当前的域名.

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.

推荐答案

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

Mark the object that you want to use for cross appdomain communication as MarshalByRefObject.

这篇关于如何从另一个应用程序域调用类的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-17 22:04:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/922708.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!