基于接口继承的函数重载

Function overloads based on interface inheritance

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

Public Interface Parent
    {stuff}
End Interface

Public Interface Child
    Inherits Parent
    {other stuff}
End Interface

我也有一个 sub 和它的重载 :

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

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

如果我这样调用 sub,它会调用 "child function" :

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))

这将在运行时转换为 "real" 类型,有效地调用正确的函数。但是,所有函数都必须 public 才能以这种方式调用