Search code examples
.netconsole-applicationbuildmanager

.net console app lifecycle - working around a pre-start initialization error from BuildManager.GetReferencedAssemblies


I'm trying to iterate through the referenced assemblies in my console app. I have been doing this with BuildManager.GetReferencedAssemblies in other projects, but in my console application, I get an InvalidOperationException: This method cannot be called during the application's pre-start initialization stage.

To my knowledge, there isn't a way to delay execution in a console app. You do it in static void Main, or you don't do it at all... Anyone have any suggestions on how to get around this?


Solution

  • AppDomain.CurrentDomain.GetAssemblies() didn't quite do the trick. There were some assemblies that were not included, though they were referenced. What I did was this:

    allAssemblies = (from name in Assembly.GetEntryAssembly().GetReferencedAssemblies()
            select Assembly.Load(name)).ToList();
    

    This didn't return as many assemblies as AppDomain.CurrentDomain.GetAssemblies(), but it returned the ones I needed. Not sure why there was a discrepancy. Cerebrate, thank you for the reply though. You got me on the track I needed, and I upvoted your reply.