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)
Community
  • 1
  • 1
Keith
  • 133
  • 11
  • 2
    And what exactly is the exception? The compiler does indeed generate an extra type per async method, for the state machine. But without any detail about what the exception is, we can't really guess what's wrong. – Jon Skeet Feb 06 '17 at 18:27
  • I have just added the exception being thrown. – Keith Feb 07 '17 at 14:36
  • Did you read the message though? "Retrieve the LoaderExceptions property for more information." - so do that, and include that in the question too... – Jon Skeet Feb 07 '17 at 14:40
  • Updated the LoaderExceptions message. – Keith Feb 07 '17 at 15:03
  • 1
    Okay, so it looks like you've ended up with a reference to the PCL version of System.Core somehow. I suspect something's gone wrong with the Microsoft.Bcl.Async package installation... do you *have* to target .NET 4 rather than 4.5? With the latter you wouldn't need the extra package, and life would be simpler. – Jon Skeet Feb 07 '17 at 15:14
  • Thanks for your help. I confirm that the issue was only occurring on .NET 4 machines. I have applied the [KB2468871](http://stackoverflow.com/a/24514073/2646437) patch and that also solves the issue. – Keith Feb 07 '17 at 15:30

0 Answers0