Q:

Silverlight 4 Unit Testing; Wrong Version of mscorlib is loaded

I have been trying to develop some unit tests for a Silverlight 4 project in MSTest but I am having some trouble getting them to run.  In my code, I am using some expressions (System.Linq.Expressions) and they are working correctly when I run the program under normal circumstances.  If any of this code is encountered during a unit test however, I get a 'MissingMethodException' on the type 'AppDomain'
I have traced the issue to the fact that 'AppDomain' is defined in mscorlib.dll.  When I run the program normally, version 2.0.5.0 of mscorlib (Silverlight 4) is loaded, and under unit tests version 2.0.0.0 is loaded.  All of the references in both my test and normal project are correct so I can't really figure out why MSTest is loading the wrong version.
Is there some way to get MSTest or VSTestHost to actually load the proper version of mscorlib? (2.0.5.0)

A:

The problem is that you are using one version of the .NET framework to run the tests, and another to run your code under normal circumstances. Silverlight and .NET are compatible enough that Silverlight assemblies can often be used from .NET without problems (even if Visual Studio gives you a warning on the reference), but there are a few exceptions.
For example, running Silverlight code this way, you cannot use any of the Silverlight UI classes. (Any FrameworkElement, DependencyObject, Dispatcher or similar) as these require the correct runtime. I am not surprised that AppDomain also falls into this category.
To work around this problem, you can either make sure to only test code that has no dependencies on classes like the above, or you can cross compile your source code as full .NET projects. The latter does not solve the incompatibilities for you, but allows you to compile slightly different versions of your code to let the code compile and run cleanly.
Note that mscorlib is not a .NET assembly like any other, it's full of nasty native code references and cannot be used in any meaningful way outside of its intended runtime.

