ReflectionTypeLoadException when async methods are present

2

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?

  • My development machine is: Windows 10 - CLR version: 4.0.30319.42000
  • My problematic machine is: Windows 7 x86 - CLR version: 4.0.30319.1
  • I am targeting .NET4.0 and using the Microsoft.Bcl.Async Nugget package

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)
c#
system.reflection
asked on Stack Overflow Feb 6, 2017 by Keith • edited May 23, 2017 by Community

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0