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.

line keyword in CSharp

I was working on some WPF code today and ended up looking in the partial class generated for a UserControl I was working on. The code it was generating was very strange and littered throughout it were some #line statements. So I headed over to the #line MSDN article to see what it was all about. It turns out it is a way for you to tell the compiler, directly in your code, to pretend that a particular line of code is actually from somewhere else. You can also “hide” code from the debugger apparently.

From what I understand this is only useful for compile time errors, and even then probably only useful for code generation. I’m pretty excited about this though because it will really be useful for generating code in NBusiness. In this way if I generate a property (for example) based on a field declared in an entity I can add a #line statement to redirect any errors that come up back to the entity field rather than some murky, generated, partial class that no one want’s to look through.

Beyond uses for code generation I really have a hard time coming up with a practical reason why it would be used… but it seems like it could be a really fun way to obfuscate code and make your coworkers life a living hell. Maybe if you don’t like a particular piece of code they wrote in, say, GiantComponenent.cs just add a snippet to one of your classes such as:

#line 1 "GiantComponent.cs"
)()F@#K@N3fkn

 

Now just head over to their desk and complain about some compile error you’re running into in their code.

Well I guess it’s true that you learn something new everyday.