繁体   English   中英

基于接口继承的函数重载

[英]Function overloads based on interface inheritance

说我有一个基本接口,另一个有继承自它:

Public Interface Parent
    {stuff}
End Interface

Public Interface Child
    Inherits Parent
    {other stuff}
End Interface

我也有一个子及其超载:

Public Sub doStuff(parameter As Parent)
     {do some stuff}
End Sub 

Public Sub doStuff(parameter As Child)
     {do some other stuff}
End Sub 

如果我这样调用子程序,它将调用“子函数”:

Dim myParam As Child = New SomeClassImplementingChild
doStuff(myParam)

但是,有什么方法可以让它用这样的东西来调用“子函数”,假设我在编译时不知道coolParameter的类型:

Public Sub coolFunction(coolParameter As Parent)
    doStuff(coolParameter)
End Sub 

Dim myParam As Child = New SomeClassImplementingChild
coolFunction(myParam)

我找到了一种解决方法,我想我将最接近一种解决方案:

doStuff(Convert.ChangeType(coolParameter, coolParameter.GetType))

这将在运行时转换为“真实”类型,从而有效地调用正确的函数。 但是,所有功能都必须公开才能以这种方式调用

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM