.NET控制台应用程序生命周期 - 从周围一个BuildManager.GetReferencedAssemblies pre-启动初始化错误工作 [英] .net console app lifecycle - working around a pre-start initialization error from BuildManager.GetReferencedAssemblies
问题描述
我想遍历引用的程序集在我的控制台应用程序。我一直在做这与其他项目BuildManager.GetReferencedAssemblies,但在我的控制台应用程序,我得到一个InvalidOperationException:此方法不能在应用程序的pre-启动初始化阶段被称为
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?
推荐答案
AppDomain.CurrentDomain.GetAssemblies()并没有完全达到目的。有不包括某些组件中,虽然他们引用。我所做的是这样的:
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();
这并没有返回尽可能多的组件为AppDomain.CurrentDomain.GetAssemblies(),但它返回的那些我需要的。不知道为什么出现了差异。脑虫,但感谢你的回复。你有我,我需要在赛道上,我upvoted你的答复。
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.
这篇关于.NET控制台应用程序生命周期 - 从周围一个BuildManager.GetReferencedAssemblies pre-启动初始化错误工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!