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

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

问题描述

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

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天全站免登陆