Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Unit Tests fails when running test without the debugger

My unit tests fails when they get here:

var y = AppDomain.CurrentDomain
                 .GetAssemblies()
                 .Where(a => !a.GlobalAssemblyCache)
                 .SelectMany(a => a.GetExportedTypes()
                                   .Where(t => t.IsClass && typeof(ITypeRenderer).IsAssignableFrom(t)))
                 .ToArray();

But when I run the test with the debugger attached, it does not fail.

I guess it has to do something with reflection permissions and all that jazz, any idea?

The exception is:

Initialization method MyUnitTestProject.UnitTest.Init threw exception. System.NotSupportedException: System.NotSupportedException: The invoked member is not supported in a dynamic assembly..

The exception happens at:

System.Reflection.Emit.InternalAssemblyBuilder.GetExportedTypes()

Thanks.

like image 258
vtortola Avatar asked Jan 21 '26 00:01

vtortola


1 Answers

Well, the error message does say "The invoked member is not supported in a dynamic assembly". Obviously, when you're running the tests outside of the debugger, something is causing a dynamic assembly to be created in the same AppDomain as where the tests are running.

Equally (hopefully) obviously, a check for IsDynamic to exclude such assemblies (in the same way that you exclude GAC assemblies) should avoid the error.

like image 190
Damien_The_Unbeliever Avatar answered Jan 22 '26 14:01

Damien_The_Unbeliever



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!