简体   繁体   English

为什么我可以将错误的接口传递给 function 而没有编译器错误?

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

Curious if I've messed up some Visual Studo settings that should be flagging this, or if I'm misunderstanding something entirely.好奇我是否弄乱了一些应该标记此问题的 Visual Studo 设置,或者我是否完全误解了某些东西。

I have a funcion A which has 2 very basic interfaces as parameters:我有一个函数 A,它有 2 个非常基本的接口作为参数:

  1. oConfiguration as IConfiguration o配置为 IConfiguration
  2. oEventLogHandler as IEventLogHandler oEventLogHandler 作为 IEventLogHandler

Function B has a single parameter: Function B 有一个参数:

  1. oEventLogHandler as IEventLogHandler oEventLogHandler 作为 IEventLogHandler

Function A calls Function B, passing oConfiguration as an argument. Function A 调用 Function B,将 oConfiguration 作为参数传递。 I don't seem to get any compiler errors or warnings in Visual Studio but of course I get the runtime error:我似乎在 Visual Studio 中没有收到任何编译器错误或警告,但我当然会收到运行时错误:

Unable to cast object of type 'ConfigurationImplementationA' to type 'IEventLogHandler'.无法将“ConfigurationImplementationA”类型的 object 转换为“IEventLogHandler”类型。

Turn on Option Strict .打开Option Strict

It's off by default to support legacy VB6 code.它默认关闭以支持旧版 VB6 代码。 Otherwise, it's just pain and suffering to leave it off, so don't.否则,离开它只是痛苦和痛苦,所以不要。

See also: What do Option Strict and Option Explicit do?另请参阅: Option Strict 和 Option Explicit 有什么作用?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么此子项有效? 为什么我可以将每个类型(字符串,接口,整数)传递给子对象而没有编译器错误作为参数 - Why this sub is valid? Why i can pass each Type (String, Interface, Int) to a sub without compiler error as parameter 为什么我不能在接口上调用ToString()? - Why can't I call ToString() on an interface? 我可以将匿名类型作为参数传递给函数吗? - Can I pass an anonymous type as a parameter to a function? 如何将计时器 object 传递给 function? - How can i pass a timer object to a function? 为什么这个 IsNum 函数是错误的? - Why is this IsNum function wrong? 为什么我不能将我的具体类型重铸为界面 - Why can't I recast my concrete type as an interface 为什么当我使用 aspnet_compiler.exe 编译我的网站时会出现构建错误,而当我使用 VS 的构建工具时却不会? - Why do I get build errors when I compile my site with aspnet_compiler.exe, but not when I VS's build tool? 为什么我可以将Nullable值类型传递给不可为null的值类型? - Why can I pass a Nullable valuetype into a non nullable valuetype? 当需要 Long 类型的参数时,为什么我可以通过 Integer? - Why can I pass an Integer when a param of type Long is expected? 我可以将类引用作为参数传递给VBNet中的函数吗? - Can I pass a class reference as a parameter to a function in VBNet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM