Convert string value to type and pass as type parameter. RRS feed

  • Question

  • User-379429695 posted


    In the below code... i am getting all filenames from a directory.  parsing out the directory names & the .vb extension. . . then converting those filesnames to classes.  Every file in the LPCriteria direcotry is its own class with a "Evaluate" function.

    The problem comes in here:

    Dim boolFlagged As Boolean = CType(MyInstance, objType).Evalua

    Because the "objType" comes up as "type 'objType' is not defined".   the parameter requested is "Object as Type".  But i dont know the type, as it is really the name of the file...       any ideas how i get this parameter passed in?   Thanks! :)

    Dim strPath As String = System.AppDomain.CurrentDomain.BaseDirectory()
    Dim files As String() = System.IO.Directory. _
         GetFiles(strPath & "App_Code\GeneralClasses\LPCriteria\", "*")
                For Each fileName As String In files
                    fileName = fileName.Remove(0, fileName.LastIndexOf("\") + 1). _
                                 Replace(".vb", "")
                    Dim MyInstance As Object = Activator.CreateInstance(Type.GetType(fileName))
                    Dim objType As Type = MyInstance.GetType
                    Dim boolFlagged As Boolean = CType(MyInstance, objType).Evaluate _
                          (m_AnOrder, dsTransactionsForBillingAddress, dsTransactionsForShippingAddresses, dsTransactionsForCards)






    Friday, November 19, 2010 6:04 PM