Reputation: 27833
In my project I have the following helper method, which goes through all the assembly and gets all types that are subclasses of the BaseCamaFrom type.
public static List<Type> GetAllTestActionFormTypes()
{
List<Type> types = new List<Type>();
// add all the types that are subclasses of BaseCamaForm to the _camaFormType list
foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
foreach (Type t in asm.GetTypes())
if (t.IsSubclassOf(typeof(BaseCamaForm)))
types.Add(t);
return types;
}
This method works correctly on the first call. However, upon calling this method a second time the following exception occurs when the asm.GetTypes()
is called:
ReflectionTypeLoadException was unhandled by user code: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
Upon looking at the LoaderException property I found a System.IO.FileLoadException
with the following message:
Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.
Why does this code work the first time it is called but always exceptions the second time?
Activator.CreateInstance(types[x])
call on some classes.
Upvotes: 2
Views: 1158
Reputation: 27833
Apparently I had to add <startup useLegacyV2RuntimeActivationPolicy="true" />
into my app.config file. Once I did that I no longer had reflection exceptions occurring. I still don't know why it did this but at least it's fixed.
Upvotes: 2