I need to do something like this in c# (pseudo):
static var ns = new Non_Serializable_Nor_Marshal()
var app = new AppDomain();
app.execute(foo)
void foo()
{
var host = AppDomain.Current.Parent; //e.g. the original one
host.execute(bar)
}
void bar()
{
ns.Something();
}
IOW I have a non serializeable nor marshal object in one appdomain.
I want to create a second domain and execute foo(). From within that second domain I want to execute bar() on the original domain.
How do I pass the original domain to the child one?
If you don’t want to use interop, you can also use a little trick using AppDomainManager. You can basically automatically ‘wire’ the ‘primary’ domain into any domains automatically – albiet the way I do it means you discard your real primary domain.
Here is the class that does all the magic:
And you need to alter your
Main()(application entry) slightly:Now at any point you can get
PrimaryAppDomainManager.PrimaryDomainto get a reference to the primary domain, just remember that it isn’t the inital domain created by the .Net runtime – it’s one we create immediately.You can look at the comments in my blog post for an way to get the .Net runtime to hook this in for you automatically using the app.config.
Edit: I forgot to add the extension method I use, here it is: