问题
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);
}
Setting vars globaly for AppDomain don't change anathing, same error.
AppDomain.CurrentDomain.SetData("client", client);
AppDomain.CurrentDomain.SetData("engine", engine);
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?
回答1:
If you show us the correct code then you can't set non-static and without instance method to DoCallBack()
.
The method should be a static:
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);
}
or you have to create a instance before pass into DoCallBack()
var instance = new Program();
appDomain.DoCallBack(instance.testFunc);
来源:https://stackoverflow.com/questions/37286078/how-to-setdata-to-new-appdomain