Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppDomain.CurrentDomain.GetAssemblies fails with ReflectionTypeLoadException

Tags:

c#

reflection

During unittesting I have run into a problem with the following code that asks for all the loaded assemblies:

var res = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(x => x.GetTypes())
.ToList();

this code fails with a ReflectionTypeLoadException which has inner exceptions of the pattern

Could not load type Microsoft.Xml.Serialization.GeneratedAssembly.FOO

where FOO are some specific classes also coded by us.

The problem arises when running unittests prior to the above which creates XML documents using the XDocument class.

I may not necesarilly want to load these code generated classes (I'm guessing Microsoft.Xml.Serialization.GeneratedAssembly.* is code generated.) I just want to understand whats wrong.

like image 615
Carlo V. Dango Avatar asked Sep 26 '13 09:09

Carlo V. Dango


1 Answers

You should check IsDynamic field of your assembly which is availabe at .netframework 4 and later.

var res = AppDomain.CurrentDomain.GetAssemblies().Where(ass => ass.IsDynamic == false)
.SelectMany(x => x.GetTypes())
.ToList();
like image 124
fasadat Avatar answered Oct 28 '22 04:10

fasadat