How to cast object of type 'especific' to type 'FileHelpers.Events.INotifyRead in Multirecording

时间:2015-06-15 14:46:57

标签: vb.net filehelpers

I'm trying to centralize all formatting and conversion rule in a single class especific. Using the interface INotifyRead(Of T As Class). When I implement the methods BeforeRead/AfterRead throws an exception: Unable to cast object of type 'Especific' to type 'FileHelpers.Events.INotifyRead`1[System.Object]'.

Below my code.

Using engine As New MultiRecordEngine(New RecordTypeSelector(AddressOf CifraRecordTypeSelector),
                                                GetType(RemessaRegistroCliente),
                                                GetType(RemessaRegistroContrato))
    Dim records = engine.ReadFile(_camArquivo)
End Using

Public NotInheritable Class RemessaRegistroCliente
    Implements INotifyRead(Of RemessaRegistroCliente)

    Public Sub AfterRead(e As AfterReadEventArgs(Of RemessaRegistroCliente)) Implements INotifyRead(Of RemessaRegistroCliente).AfterRead

    End Sub

    Public Sub BeforeRead(e As BeforeReadEventArgs(Of RemessaRegistroCliente)) Implements INotifyRead(Of RemessaRegistroCliente).BeforeRead

    End Sub
End Class

Public NotInheritable Class RemessaRegistroContrato
    Implements INotifyRead(Of RemessaRegistroContrato)

    Public Sub AfterRead(e As AfterReadEventArgs(Of RemessaRegistroContrato)) Implements INotifyRead(Of RemessaRegistroContrato).AfterRead

    End Sub

    Public Sub BeforeRead(e As BeforeReadEventArgs(Of RemessaRegistroContrato)) Implements INotifyRead(Of RemessaRegistroContrato).BeforeRead

    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

由于MultiRecordEngine没有通用版本,因此您无法实现通用INotifyRead(Of T)来处理事件。而是将代理人分配给引擎。

Sub Main()
    Using engine As New MultiRecordEngine(New RecordTypeSelector(AddressOf CifraRecordTypeSelector),
                                                GetType(RemessaRegistroCliente),
                                                GetType(RemessaRegistroContrato))
        AddHandler engine.BeforeReadRecord, AddressOf BeforeReadRecordHandler
    End Using
End Sub

Private Sub BeforeReadRecordHandler(ByVal engine As EngineBase, ByVal e As BeforeReadEventArgs(Of Object))

End Sub

您还需要修改类以删除通用接口:

Public NotInheritable Class RemessaRegistroCliente
   'your fields
End Class

Public NotInheritable Class RemessaRegistroContrato
   'your fields
End Class