如何将数据设置为新的 Appdomain [英] How to Setdata to new Appdomain
问题描述
如何将数据设置为新创建的 AppDomain.当我 DoCallBack 到我的 testFunc 时,我收到System.NullReferenceException"异常.我做错了什么?
How to SetData to new created AppDomain. When i DoCallBack to my testFunc i receive "System.NullReferenceException" exception. What i do wrong?
var client = "test";
var engine = 123;
AppDomain appDomain = AppDomain.CreateDomain("newDomain");
appDomain.SetData("client", client);
appDomain.SetData("engine", engine);
appDomain.DoCallBack(testFunc);
private void testFunc()
{
var client = (string)AppDomain.CurrentDomain.GetData("client");
var engine = (int)AppDomain.CurrentDomain.GetData("engine");
Console.WriteLine("client: " + client);
Console.WriteLine("engine: " + engine);
}
为 AppDomain 设置全局变量不会改变 anathing,同样的错误.
Setting vars globaly for AppDomain don't change anathing, same error.
AppDomain.CurrentDomain.SetData("client", client);
AppDomain.CurrentDomain.SetData("engine", engine);
附言我收到 System.NullReferenceException,因为 AppDomain 找不到我在 DoCallBack 之前设置的变量.那么如何以正确的方式设置它们?
P.S. I receive System.NullReferenceException, because AppDomain can't find that vars that i was setup before DoCallBack. So how to setup them in right way?
推荐答案
如果您向我们展示了正确的代码,那么您就不能将非静态和无实例方法设置为 DoCallBack()
.
If you show us the correct code then you can't set non-static and without instance method to DoCallBack()
.
该方法应该是静态的:
private static void testFunc()
{
var client = (string)AppDomain.CurrentDomain.GetData("client");
var engine = (int)AppDomain.CurrentDomain.GetData("engine");
Console.WriteLine("client: " + client);
Console.WriteLine("engine: " + engine);
}
或者你必须在传入DoCallBack()
var instance = new Program();
appDomain.DoCallBack(instance.testFunc);
这篇关于如何将数据设置为新的 Appdomain的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!