2

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!

Community
  • 1
  • 1
LolCat
  • 539
  • 1
  • 11
  • 24
  • And what kind of problem are you trying to solve? So far your scenario seems like there won't be any real need to have so many different app domains. – Ondrej Tucny Aug 07 '12 at 18:58
  • @OndrejTucny I am finding resources files from a given assembly. But I always get stuck with the "already loaded in appdomain" error. So I tought I could use another AppDomain and unload it when I'm done. – LolCat Aug 07 '12 at 18:59
  • If you explain what you're trying to accomplish, someone might be able to help. using `typeof(typename)` means that the assembly that contains `typename` must be loaded in the main appdomain (circumventing any need to load another). i.e. we don't know what you're trying to accomplish, so we can't really tell you what to put there. – Peter Ritchie Aug 07 '12 at 19:50

1 Answers1

3

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.

Chris Hannon
  • 4,134
  • 1
  • 21
  • 26