mardi 17 juillet 2018

Need a way around using generic methods and resolving at run-time

I have to create a function that does the following:

Step 1 uses reflection to find the generic argument types of a KeyValuePair class property.

Step 2 extracts string values from an XML document and pass them to a deserializing method to create the expected KeyValuePair with the correct types discovered previously in the first step (it parses individual string values from a comma-separated string list and converts each string value to the expected type).

The problem: I am having difficulties with the code structure because you cannot resolve generic methods at runtime. This leads to large if-else-statements and it makes the code very unscalable as more permutations of key and value types are added.

Is there any nicer way of dealing with this?

Private Shared Function GetKeyValuePairs(propertyInfo As PropertyInfo, propValue As String) As Object
    Dim valueType As String
    Dim keyType As String

    Try
        'propertyInfo is info relating to a KeyValuePair property type.
        'For example: KeyValuePair(Of String, Integer) would mean 
        'valueType = "System.Int32" and keyValue = "System.String".
        valueType = propertyInfo.PropertyType.GenericTypeArguments(0).ToString().ToLower()
        keyType = propertyInfo.PropertyType.GenericTypeArguments(1).ToString().ToLower()

        If valueType.Contains("int") Then

            If keyType.Contains("int") Then
                Return CollectionsSerialiser.Deserialise(Of Integer, Integer)(propValue)

            ElseIf keyType.Contains("guid") Then
                Return CollectionsSerialiser.Deserialise(Of Guid, Integer)(propValue)

            ElseIf keyType.Contains("string") Then
                Return CollectionsSerialiser.Deserialise(Of String, Integer)(propValue)

            'etc...

            End If

        ElseIf valueType.Contains("guid") Then
            If keyType.Contains("int") Then
                Return CollectionsSerialiser.Deserialise(Of Integer, Guid)(propValue)

            ElseIf keyType.Contains("guid") Then
                Return CollectionsSerialiser.Deserialise(Of Guid, Guid)(propValue)

            ElseIf keyType.Contains("string") Then
                Return CollectionsSerialiser.Deserialise(Of String, Guid)(propValue)

            'etc...

            End If

        ElseIf valueType.Contains("string") Then
            If keyType.Contains("int") Then
                Return CollectionsSerialiser.Deserialise(Of Integer, String)(propValue)

            ElseIf keyType.Contains("guid") Then
                Return CollectionsSerialiser.Deserialise(Of Guid, String)(propValue)

            ElseIf keyType.Contains("string") Then
                Return CollectionsSerialiser.Deserialise(Of String, String)(propValue)

            'etc...

            End If

        'etc...

        End If

    Catch ex As Exception
        'Handle exception...
    End Try

End Function





Aucun commentaire:

Enregistrer un commentaire