如何将变量从一个应用程序域传递到另一个 [英] How to pass a variable from one app domain to another

查看:63
本文介绍了如何将变量从一个应用程序域传递到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,如果我有一个变量(例如字符串),如何将其值传递给新的应用程序域:

I'd like to know, if I have a variable,for example, a string, how to pass its value to my new app domain:

static string _str;

static void Main(string[] args) {
    _str = "abc";
    AppDomain domain = AppDomain.CreateDomain("Domain666");
    domain.DoCallBack(MyNewAppDomainMethod);
    AppDomain.Unload(domain);
    Console.WriteLine("Finished");
    Console.ReadKey();
}

static void MyNewAppDomainMethod() {
    Console.WriteLine(_str); //want this to print "abc"
}

谢谢

推荐答案

使用带有AppDomainSetup参数的AppDomain.CreateDomain变体之一.在AppDomainSetup对象中,将AppDomainInitializerArguments成员设置为要传递给新应用程序域的字符串数组.

Use one of the variations of AppDomain.CreateDomain that takes an AppDomainSetup argument. In the AppDomainSetup object, set the AppDomainInitializerArguments member to the string array you want passed to the new app domain.

请参见 http://msdn.microsoft上的示例代码. com/en-us/library/system.appdomainsetup.appdomaininitializerarguments.aspx .

有了问题中的代码,更改可能看起来像(未经测试):

With the code in the question, the change might look like (not tested):

static voide Main(string[] args) {
    _str = "abc";

    AppDomainSetup setup = new AppDomainSetup();
    setup.AppDomainInitializer = new AppDomainInitializer(MyNewAppDomainMethod);
    setup.AppDomainInitializerArguments = new string[] { _str };

    AppDomain domain = AppDomain.CreateDomain(
        "Domain666",
        new Evidence(AppDomain.CurrentDomain.Evidence),
        setup);

    Console.WriteLine("Finished");
    Console.ReadKey();
}

static void MyNewAppDomainMethod(string[] args) {
    ...
}

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

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆