如何将变量从一个app域传递到另一个app域

时间:2022-08-23 11:12:21

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

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

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"
}

Thanks

3 个解决方案

#1


13  

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.

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

See sample code at http://msdn.microsoft.com/en-us/library/system.appdomainsetup.appdomaininitializerarguments.aspx.

请参阅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) {
    ...
}

#2


19  

Addressing both your first and second requirements (passing through a value and retrieving another value back), here is a quite simple solution:

解决你的第一个和第二个要求(传递一个值并检索另一个值),这是一个非常简单的解决方案:

static void Main(string[] args)
{
    AppDomain domain = AppDomain.CreateDomain("Domain666");
    domain.SetData("str", "abc");
    domain.DoCallBack(MyNewAppDomainMethod);
    string str = domain.GetData("str") as string;
    Debug.Assert(str == "def");
}

static void MyNewAppDomainMethod()
{
    string str = AppDomain.CurrentDomain.GetData("str") as string;
    Debug.Assert(str == "abc");
    AppDomain.CurrentDomain.SetData("str", "def");
}

#3


6  

I know that this is an old thread but perhaps this will help other people who are researching the subject.

我知道这是一个老线索,但也许这将有助于其他研究该主题的人。

In this article, the writer suggests using application domain SetData and GetData methods for basic exchange of data objects that support marshal-by-value or marshal-by-reference object.

在本文中,作者建议使用应用程序域SetData和GetData方法进行数据对象的基本交换,这些数据对象支持按值编组或按引用编组对象。

#1


13  

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.

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

See sample code at http://msdn.microsoft.com/en-us/library/system.appdomainsetup.appdomaininitializerarguments.aspx.

请参阅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) {
    ...
}

#2


19  

Addressing both your first and second requirements (passing through a value and retrieving another value back), here is a quite simple solution:

解决你的第一个和第二个要求(传递一个值并检索另一个值),这是一个非常简单的解决方案:

static void Main(string[] args)
{
    AppDomain domain = AppDomain.CreateDomain("Domain666");
    domain.SetData("str", "abc");
    domain.DoCallBack(MyNewAppDomainMethod);
    string str = domain.GetData("str") as string;
    Debug.Assert(str == "def");
}

static void MyNewAppDomainMethod()
{
    string str = AppDomain.CurrentDomain.GetData("str") as string;
    Debug.Assert(str == "abc");
    AppDomain.CurrentDomain.SetData("str", "def");
}

#3


6  

I know that this is an old thread but perhaps this will help other people who are researching the subject.

我知道这是一个老线索,但也许这将有助于其他研究该主题的人。

In this article, the writer suggests using application domain SetData and GetData methods for basic exchange of data objects that support marshal-by-value or marshal-by-reference object.

在本文中,作者建议使用应用程序域SetData和GetData方法进行数据对象的基本交换,这些数据对象支持按值编组或按引用编组对象。