我正在尝试使用反射来获取当前正在执行的程序集的路径以用于注册某些类型。这是在静态/共享方法中调用的
Dim Path = System.Reflection.Assembly.GetExecutingAssembly.Location
此行抛出StackOverflow异常,其中包含以下详细信息:
An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll
System.StackOverflowException
Data: unable to evaluate expression.
HelpLink: unable to evaluate expression.
HResult: unable to evaluate expression.
InnerException: unable to evaluate expression.
Message: unable to evaluate expression.
Source: unable to evaluate expression.
StackTrace: unable to evaluate expression.
TargetSite: unable to evaluate expression.
正在主线程上进行调用。我正在使用.Net 4.5 / VS11 Beta
对于记录,GetEntryAssembly
,GetCallingAssembly
等...都是这样做的。我以前从未见过(甚至没有看过)这种行为 - 有人有任何建议吗?
编辑:
操作系统:Win7 x64旗舰版 这是一个Winforms应用程序
我有一个共享方法应该返回一个依赖项解析器(包装在我自己的类中以抽象它)。
Private Shared _Resolver As IDependencyResolver
Public Shared Function QuickResolver() As IDependencyResolver
If _Resolver Is Nothing Then
Dim Container = New WindsorContainer
''Line below was breaking so I exploded it into multiple lines as shown above
Dim CurrentPathFilter = New AssemblyFilter(IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly.Location))
Container.Register(
Component.For(Of Interfaces.ISomeInterface)().
ImplementedBy(Of DAL.SomeType).LifestylePerThread)
''.... More registrations ....
Dim Resolver As New WindsorDependencyResolver(Container)
_Resolver = Resolver
End If
Return _Resolver
End Function
它是singleton-ish(我知道初始化应该重构为不同的方法 - 它在我的列表中)
从Winforms UI线程调用它如下:
Resolver = Common.DependencyResolverFactory.QuickResolver
ScanRepository = Resolver.Resolve(Of IRepository(Of Scan))()
异常是由GetExecutingAssembly
行抛出的(那是异常中断的地方)我承认这是不寻常的,所以我假设我的代码使它接近SO并且GetExecutingAssembly方法深度嵌套到溢出?
答案 0 :(得分:1)
使用以下示例代码获取程序集目录。
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location))
或
Path.GetDirectoryName(typeof(Foo).Assembly.ManifestModule.FullyQualifiedName)