views:

89

answers:

2

Hi,
I use the default appdomain (AD) which I use to create new appdomains (AD1) when required for running plugins in isolation. When creating the new domain I also wire up the AppDomainUnload event to allow me to call clean up code etc.

The issue I seem to have is:

1) Create AD1 from AD

2) Run code in AD1

3) Call AD.Unload(AD1)

The code switches to AD1 and calls the unloading event passing in a reference to the current AppDomain (AD1).

At this point I'd like to get a reference to the current instance running in AD1 to call a shutdown method however there is no GetInstance on the AppDomain class.

Any ideas how I can go about getting it?

+1  A: 

Are you looking for AppDomain.CurrentDomain?

Jon Skeet
This thread is a perfect answer to Kirill's question about whether or not we ever run into each other on SO. He's sitting next to me.
JaredPar
I'm sure it happens the opposite way round just as often :)
Jon Skeet
Ha ha, Jared typed in his answer, hit refresh and bang! :)
Kirill Osenkov
Don't think so as I already know which domain I'm in as it's passed as an argument to the AppDomainUnload that the code runs. So whilst I know the domain I can't get a handle to the running instance of my plugin.
Spanners
@Spanners: Ah, so you're not after the instance of the *domain*, you're after the instance of some other type? What guarantee is there that you'll only have one instance *of* that type within the domain? Could you use a static variable? (There's effectively a separate static variable per AppDomain.) Better still, could the plug-in creation code not register the clean-up handler itself? It obviously knows the instance it's created...
Jon Skeet
Hadn't thought of it like that. Thanks Jon!
Spanners
A: 

When you create the instance in AD1, you could either store the instance in a static variable in the AppDoman, statics are scoped to the AppDomain. Alternatively if this is not a singleton type object, you can maintain a List<> of the objects in the AppDomain.

You could put this "registration" in the constructor of your plug-in instance. When handling the Unload you can use either the static or enumerate the list and perform the required clean-up on the object(s).

Chris Taylor