Search code examples
vb.netoopgeneric-programming

Genetic Parameters in Constructor


I have the following loop in RunWebTest method which can work with different type of classes passed to it. Now the issue is that I don't know how to modify this code so I could pass different parameters type to the constructor of the class:

Public Class TestManager
    Inherits ThreadedWebTest
    Public Property ServerURL As String

    Public Overrides Sub Run()
        RunWebTest(New Login(Users.CustomerTradeAdvanced))
        RunWebTest(New CustomerCreate)
        RunWebTest(New Logout)
    End Sub

    Private Sub RunWebTest(Of WebTestType)(test As WebTestType)
        For Each r As WebTestRequest In IncludeWebTest(GetType(WebTestType).GetConstructor(New System.Type() {}).Invoke(New Object() {}), False)
            MyBase.Send(r)
        Next
    End Sub
End Class

I'd appreciate your help


Solution

  • The key was to use the base class as the parameter of the method. This code works now:

       Public Overrides Sub Run()
            RunWebTestRequests(New Login(Users.CustomerTradeAdvanced))
            RunWebTestRequests(New CustomerCreate)
            RunWebTestRequests(New SalesInvoice)
            RunWebTestRequests(New Logout)
        End Sub
    
        Private Sub RunWebTestRequests(webTest As ThreadedWebTest)
            For Each r As WebTestRequest In IncludeWebTest(webTest, False)
                MyBase.Send(r)
            Next
        End Sub