lundi 28 août 2017

GetAssemblies() throws LoaderExceptions after some changes in project

I have a method in a reflection helper class referenced in some points of the app:

public static IEnumerable<TypeInfo> GetControllersWeb()
{
    return AppDomain.CurrentDomain
                    .GetAssemblies()
                    .First(x => x.FullName.StartsWith("MyProject.Web,", StringComparison.CurrentCultureIgnoreCase))
                    .DefinedTypes
                    .Where(x => x.Name.EndsWith("Controller", StringComparison.CurrentCultureIgnoreCase));
}

Somebody made a change(removed some classes) in the project couple days ago and now this methods throws an exception. The exceptions is the following:

Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.

And in the LoaderExceptions it describes one of the removed classes.

If I change the linq query to:

AppDomain.CurrentDomain
         .GetAssemblies()
         .First()
         .DefinedTypes;

It works fine. I want to know what in that First() method could be causing this? Perhaps something that broke the project settings?





Aucun commentaire:

Enregistrer un commentaire