I am currently getting all of my class types in my .NET desktop application using:
Type[] types = Assembly.GetExecutingAssembly().GetTypes();
The problem is that on certain machines this line actually throws a ReflectionTypeLoadException.
System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
at System.Reflection.RuntimeModule.GetTypes()
at System.Reflection.Assembly.GetTypes()
at MyProgram.Package.Class.?????????????????????????????????????????(Assembly )
at MyProgram.Package.Class..cctor()
I can get around this by simply catching the exception and getting valid types in this manner:
try
{
types = Assembly.GetExecutingAssembly().GetTypes();
return types;
}
catch (ReflectionTypeLoadException e)
{
types = e.Types.Where(t => ((t != null))).ToArray();
return types;
}
Upon further analysis I noticed that on my problematic machine SystemClassName types are present as expected but SystemClassName+d__n types (which are included on my normal machine) are being excluded by the exception handler above.
I have no idea why these are generated but I noticed that these method name types are only generated for methods marked as async. If I remove all async keywords from method names (and manually call .Wait() on these methods) then the build works fine on the problematic machine without the need for any exception handling.
Can anyone explain what is happening please?
Update: Printing out the LoaderExceptions property gives the following:
Could not load file or assembly 'System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)
User contributions licensed under CC BY-SA 3.0