How to share an object across app domains in c# if the class is not serializable

混江龙づ霸主 提交于 2020-05-17 08:49:22

问题


For context, this is a follow-up to this question:

How can I reload a class in .net

To save the trouble of re-reading that post, my "high-level" problem is I want to do this:

while(true) {
    sleep(naptime);
    reload_code(); // use magic?
    do_something_useful();
}

Where do_something_useful is compiled separately from the code in the while loop, and reload_code would pick up the latest (compiled) version. I was given a link to another answer on this site, which basically said

  1. do_something_useful should be compiled as a separate assembly
  2. The while loop should load that assembly into a temporary appdomain, and unload the entire appdomain inside reload_code.

After some flailing around with mysterious exceptions, I finally got that to work for my toy example. But for my non-toy example, I had code that looked more like this:

object handle = init(); // get a handle to draw on the screen, or something
while(true) {
    sleep(naptime);
    reload_code(); // use magic?
    do_something_useful(handle);
}

It turns out that the handle object is not serializable, so the call to do_something_useful fails. This makes me wonder if what I'm doing just can't be done in C# on the CLR, and I should try some other language. (On the JVM, this would require messing with class loaders, but classes with different loaders are in one big happy family; there's no rigid boundary like app domains seem to enforce. In C, one could simply load and unload shared libraries. I expected C# to have some way to do this.)

来源:https://stackoverflow.com/questions/61699424/how-to-share-an-object-across-app-domains-in-c-sharp-if-the-class-is-not-seriali

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