问题
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
do_something_useful
should be compiled as a separate assembly- 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