0

我想实现通用方法来读取两种类型的 CSV 文件。我创建了两个类来表示每个 csv 文件。谁能告诉我如何编写通用函数 ReadFile(Of T) 并生成对象。

谢谢

我正在添加为此创建的功能。请告知实现这一目标的正确方法。

 Public Function ReadFile(Of THeader, TRecord, TTrailer, TResult)(fileName As String) As IData(Of TRecord)
        Dim objHeader As IHeader = Nothing
        Dim objTrailer As ITrailer = Nothing
        Dim objRecord As TRecord = Nothing
        Dim colRecords As Collection(Of TRecord) = Nothing
        Dim objData As IData(Of TRecord) = Nothing

        Using objDataReader As OleDbDataReader = objTextReader.Read("SELECT * FROM [" & fileName & "] WHERE RecordType='A'")
            If objDataReader.HasRows Then
                While objDataReader.Read
                    objHeader = Activator.CreateInstance(Of THeader)()

                    With objHeader.GetType
                        .GetProperty("ReportType").SetValue(objHeader, objDataReader(1).ToString, Nothing)
                        .GetProperty("FileDate").SetValue(objHeader, objDataReader(2).ToString, Nothing)
                        .GetProperty("FileTime").SetValue(objHeader, objDataReader(3).ToString, Nothing)
                    End With
                End While
            End If
        End Using

        Using objDataReader As OleDbDataReader = objTextReader.Read("SELECT * FROM [" & fileName & "] WHERE RecordType='B'")
            colRecords = New Collection(Of TRecord)
            Dim objPropInfo() As PropertyInfo

            If objDataReader.HasRows Then
                While objDataReader.Read
                    objRecord = Activator.CreateInstance(Of TRecord)()
                    objPropInfo = objRecord.GetType.GetProperties()

                    For Each propInfo In objPropInfo
                        If propInfo.Name <> "FormattedAccountTitle" And propInfo.Name <> "FormattedParticipantName" Then
                            'propInfo.SetValue(objRecord, If(objDataReader(propInfo.Name) Is DBNull.Value, "", objDataReader(propInfo.Name)), Nothing)
                            propInfo.SetValue(objRecord, objDataReader(propInfo.Name).ToString, Nothing)
                        End If
                    Next

                    colRecords.Add(objRecord)
                End While
            End If
        End Using

        Using objDataReader As OleDbDataReader = objTextReader.Read("SELECT * FROM [" & fileName & "] WHERE RecordType='Z'")
            If objDataReader.HasRows Then
                While objDataReader.Read
                    objTrailer = Activator.CreateInstance(Of TTrailer)()

                    With objTrailer.GetType
                        .GetProperty("RecordCount").SetValue(objTrailer, objDataReader(1).ToString, Nothing)
                    End With
                End While
            End If
        End Using

        objData = Activator.CreateInstance(Of TResult)()

        With objData.GetType
            .GetProperty("Header").SetValue(objData, objHeader, Nothing)
            .GetProperty("Records").SetValue(objData, colRecords, Nothing)
            .GetProperty("Trailer").SetValue(objData, objTrailer, Nothing)
        End With

        Return objData
    End Function

以下是示例 CSV 文件:

CSV1:

A,T18,04SEP2013,101540,,,,,,,,,,,,,,,,,,,
B,012345678,2013,,,,,,,,,,,,,,,,,,,,
B,,,1234567890,Mr Johnathan Peter Jones,Mr,Johnathan Peter,Jones,,Y,2,1,2,"Griffin House, Silver Head Street",Sheffield,,,,12A456,ZZ,01041960,10245.45,2548.15
B,,,1234567890,Miss Jane Elizabeth Smith,Mr,Johnathan Peter,Jones,,Y,2,2,2,"Griffin House, Silver Head Street",Sheffield,,,,12A456,ZZ,01041960,10245.45,2548.15
B,,,1234567899,Mr JSimon Soanes,Mr,Simon,Soanes,,Y,1,1,1,Abc Building Xyz street,Sheffield,,,,12AB516,ZZ,01091966,88245.45,4148.15
Z,3,,,,,,,,,,,,,,,,,,,,,,

CSV2:

A,T17,17SEP2013,143021,,,,,,,,,,,,,,,,,,,,,,,,
B,1SDDFDDGT,,2013,,,,,,,,,,,,,,,,,,,,,,,,
B,,,400109,1234FFFFFFFFFFF,Samsung Mobiles,,,,Samsung,N,1,1,1,Samsung Corporation Private Limited,"West Avenue, 4th Division, Devonshire Lane",,,United Kingdom,258DFF,ZZ,21524154 ,,123888.25,2562.78,Y,N
B,,,400120,1234FSDUFFFFFFF,Sony Mobiles,,,,Sony,N,1,1,1,Sony Corporation Private Limited,,,,United Kingdom,255DFF,ZZ,216666154,,123888.25,2562.78,N,N
Z,2,,,,,,,,,,,,,,,,,,,,,,,,,
4

0 回答 0