AppDomain TransparentProxy Objects of Different Type

I have a very perplexing situation related to AppDomains. I am trying to create a new AppDomain (so I can load arbitrary assemblies and unload them again) but I am unable to cast returned objects to Types that I am able to use.

Specifically I have an object of Type EntityField[] being returned. If I set it to an object I can see that the Type is correct by calling GetType() in the watch window however whenever I cast it from an object to a strongly typed array I get a null value. Here is a little debugger screen shot.

I believe what is happening is that my assembly is being loaded into both AppDomains. So, while the objects are able to correctly identify themselves as the correct types it seems that the runtime is interpretting them as different types, despite the fact that they’re coming from the exact same Assembly (even the same file path).

What is the solution to this problem?? Should I not be loading the assembly into both domains? It seems to do this automatically when I try to create the proxy instance. One interesting thing to note is that I had to add an event handler for AppDomain.Current.AssemblyResolve, which I then loop through the local assemblies and return the one who’s fullname matches the args.FullName. Is there a more correct way to resolve assemblies? Why is it not resolved automatically, without any help, to begin with?

If anybody knows I’d greatly appreciate some help figuring this out.

As a side note, I’d like to offically vote for the ability to unload assemblies from the current AppDomain. That would be a nice feature.

Author: justinmchase

I'm a Software Developer from Minnesota.

%d bloggers like this: