本文介绍了如何将数据设置为新的Appdomain的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将Data设置为新创建的AppDomain.当我对我的testFunc执行DoCallBack时,我收到" 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全局设置vars不会更改aathing,同样的错误.

Setting vars globaly for AppDomain don't change anathing, same error.

AppDomain.CurrentDomain.SetData("client", client);
AppDomain.CurrentDomain.SetData("engine", engine);

PS 我收到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()

or you have to create a instance before pass into DoCallBack()

var instance = new Program();
appDomain.DoCallBack(instance.testFunc);

这篇关于如何将数据设置为新的Appdomain的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 21:18