【问题标题】:Reflection GetExecutingAssembly is throwing a StackOverflow反射 GetExecutingAssembly 正在抛出 StackOverflow
【发布时间】:2012-05-18 21:03:17
【问题描述】:

我正在尝试使用反射来获取当前正在执行的程序集的路径,以用于注册某些类型。这是在静态/共享方法中调用的

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

郑重声明,GetEntryAssemblyGetCallingAssembly 等等……都是一样的。我以前从未见过(甚至读过)这种行为 - 有人有什么建议吗?

编辑:

操作系统:Win7 x64 Ultimate 这是一个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

它是单例的(我知道初始化应该重构为不同的方法 - 它在我的列表中)

从 Winforms UI 线程调用如下:

    Resolver = Common.DependencyResolverFactory.QuickResolver
    ScanRepository = Resolver.Resolve(Of IRepository(Of Scan))()

GetExecutingAssembly 行抛出了异常(这是异常中断的地方)我承认这很不寻常,所以我假设我的代码使其接近 SO 并且 GetExecutingAssembly 方法嵌套得足够深以至于溢出?

【问题讨论】:

  • 你如何确定溢出是在那一行而不是你自己的代码在另一行?
  • 你能告诉我们这行代码的一些上下文吗?该方法还做了哪些其他事情?
  • 您在哪个操作系统上尝试此代码。
  • 这是一个 WinForms 应用程序吗?你是从应用程序程序集而不是从某个类库程序集中调用它吗?请澄清
  • 不支持动态组装获取位置。

标签: .net exception reflection


【解决方案1】:

使用以下示例代码获取程序集目录。

System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location))

Path.GetDirectoryName(typeof(Foo).Assembly.ManifestModule.FullyQualifiedName)

【讨论】:

  • 哇...没有什么比使用 Q 中已经出现的行作为答案...IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly.Location) - 请参阅完整示例的第 6 行。也就是说,我开始认为GetExecutingAssembly 是一个红鲱鱼 - 很可能调用嵌套了一点,并且嵌套足以在其他东西已经几乎填充后最大化堆栈它
  • 您似乎能够获取路径,但第 6 行的“new AssemblyFilter()”导致错误。请将此声明分成两行。先获取路径,然后在 AssemblyFilter 构造函数中传递这个路径。
  • 感谢您的回答,但是如果您查看第一个 sn-p,将 Dim Path = System.Reflection.Assembly.GetExecutingAssembly.Location 单独放在一行上仍然会引发异常 - 我已经将它从构造函数中分离出来,以防万一问题出在哪里……
  • 不,我没有 - 但我需要了解为什么这会引发异常。还值得注意的是,第二个选项需要一个可能有问题的类型,因为此类的目的是在给定目录中注册程序集中的所有类型(至少,所有由我签名的类型),因此它必须能够处理我还不知道任何类型的程序集。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多