LolCat
LolCat

Reputation: 549

typeof new appdomain

I want to create a new AppDomain. I try doing this : Create application domain and load assembly

But I don't know what type I'm suppose to give to my domain ...

var domain = AppDomain.CreateDomain("NewAppDomain");
var path = @"C:\work\SomeAssembly.dll";
var t = typeof(SomeType);
var instance = (SomeType)domain.CreateInstanceFromAndUnwrap(path, t.FullName);

What I really want to do is to create a temporary AppDomain that load an assembly and find its references. Then I would create another AppDomain and load all the referenced assemblies and the one in the temporary AppDomain. Finaly, I would unload the temporary AppDomain and work from the other AppDomain that I can unload when I use another assembly.

My principal question is : what is "SomeType" in the code above? ... What I'm suppose to put there?

Thanks!

Upvotes: 2

Views: 154

Answers (1)

Chris Hannon
Chris Hannon

Reputation: 4134

The type in question is a proxy class that you define. It must inherit from MarshalByRefObject and both your separate AppDomain and your current AppDomain must be able to locate it.

CreateInstanceFromAndUnwrap will create an instance of that type in your separate AppDomain and then give you a __TransparentProxy in your current AppDomain that you can cast as your type. Method calls on your proxy object will be invoked in the other AppDomain on your type.

Keep in mind, though, that loading/unloading an AppDomain is incredibly expensive, especially in terms of performance and, for your specific scenario of trying to get resources from an assembly, it sounds like there's probably a better way. You may want to ask a different question about how to access your resource files appropriately instead.

Upvotes: 3

Related Questions