【问题标题】:Why can I pass the wrong interface to a function without compiler errors?为什么我可以在没有编译器错误的情况下将错误的接口传递给函数?
【发布时间】:2020-10-23 01:21:08
【问题描述】:

很好奇我是否搞砸了一些应该标记此问题的 Visual Studo 设置,或者我是否完全误解了某些内容。

我有一个函数 A,它有 2 个非常基本的接口作为参数:

  1. oConfiguration 作为 IConfiguration
  2. oEventLogHandler 作为 IEventLogHandler

函数 B 有一个参数:

  1. oEventLogHandler 作为 IEventLogHandler

函数 A 调用函数 B,将 oConfiguration 作为参数传递。我似乎在 Visual Studio 中没有收到任何编译器错误或警告,但我当然会收到运行时错误:

无法将“ConfigurationImplementationA”类型的对象转换为“IEventLogHandler”类型。

【问题讨论】:

    标签: vb.net visual-studio parameters interface arguments


    【解决方案1】:

    开启Option Strict

    默认关闭以支持旧版 VB6 代码。否则,放下它只是痛苦和痛苦,所以不要。

    另请参阅:What do Option Strict and Option Explicit do?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-12
      • 1970-01-01
      • 1970-01-01
      • 2019-02-10
      • 2015-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多