为什么我可以将错误的接口传递给一个没有编译器错误的函数?

Why can I pass the wrong interface to a function without compiler errors?

想知道我是否弄乱了一些应该标记此的 Visual Studo 设置,或者我是否完全误解了某些东西。

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

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

函数 B 有一个参数:

  1. oEventLogHandler 作为 IEventLogHandler

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

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

开启Option Strict

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

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