我真的在尝试遵循DRY原则。我有一个看起来像这样的潜艇?

Private Sub DoSupplyModel

        OutputLine("ITEM SUMMARIES")
        Dim ItemSumms As New SupplyModel.ItemSummaries(_currentSupplyModel, _excelRows)
        ItemSumms.FillRows()
        OutputLine("")

        OutputLine("NUMBERED INVENTORIES")
        Dim numInvs As New SupplyModel.NumberedInventories(_currentSupplyModel, _excelRows)
        numInvs.FillRows()
        OutputLine("")

End Sub


我想使用泛型将它们折叠为一个方法。作为记录,ItemSummaries和NumberedInventories都从相同的基类DataBuilderBase派生。

我无法弄清楚允许我在该方法中执行ItemSumms.FillRows和numInvs.FillRows的语法。

FillRows在基类中声明为Public Overridable Sub FillRows

提前致谢。

编辑
这是我的最终结果

Private Sub DoSupplyModels()

    DoSupplyModelType("ITEM SUMMARIES",New DataBlocks(_currentSupplyModel,_excelRows)
    DoSupplyModelType("DATA BLOCKS",New DataBlocks(_currentSupplyModel,_excelRows)

End Sub

Private Sub DoSupplyModelType(ByVal outputDescription As String, ByVal type As DataBuilderBase)
    OutputLine(outputDescription)
    type.FillRows()
    OutputLine("")
End Sub


但是要回答我自己的问题...我本可以做到的...

Private Sub DoSupplyModels()

    DoSupplyModelType(Of Projections)("ITEM SUMMARIES")
    DoSupplyModelType(Of DataBlocks)("DATA BLOCKS")

End Sub

Private Sub DoSupplyModelType(Of T as DataBuilderBase)(ByVal outputDescription As String, ByVal type As T)
    OutputLine(outputDescription)
    Dim type as New DataBuilderBase (_currentSupplyModel,_excelRows)
    type.FillRows()
    OutputLine("")
End Sub


那正确吗?

赛斯

最佳答案

正如其他人指出的那样,您不需要泛型即可完成所需的工作,但是我会回答技术性问题以确保完整性:

Private Sub MyMethod(Of T As DataBuilderBase)(ByVal instance As T)
    instance.FillRows()
End Sub


然后通过执行以下操作调用该方法:

MyMethod(Of ItemSummaries)(new SupplyModel.ItemSummaries(...))

10-07 14:59