How to Setdata to new Appdomain

限于喜欢 提交于 2019-12-12 00:01:56

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!