mercredi 15 juin 2016

SetValue on Propertyinfo Target Exception object doesn't match target type error vb.net

I can't get this piece of code to run correctly. I have put in breakpoints and all of the variables are getting the correct values.

    Dim reader As XmlReader = XmlReader.Create(FullName)
    Dim myType As Type = Me.GetType
    Dim propType As Type
    Dim pInfo As PropertyInfo
    Dim propertyList() As PropertyInfo = myType.GetProperties(BindingFlags.Public Or BindingFlags.Instance)
    Try

        While reader.Read()
            If reader.NodeType = XmlNodeType.Element Then
                If propertyList.Contains(myType.GetProperty(reader.Name)) Then
                    pInfo = myType.GetProperty(reader.Name)
                    propType = pInfo.PropertyType
                    reader.Read() 'Move to innerText
                    Select Case propType.Name
                        Case "Int16"
                            pInfo.SetValue(myType, CShort(reader.Value), Nothing)
                        Case Else
                            pInfo.SetValue(myType, reader.Value, Nothing)
                    End Select
                End If
            End If
        End While

Both of the SetValue statments fail with a "Object does not match target type" error. I have also tried to set myType = GetType(className). This is an instanced class and not a static class. Any ideas as to what could be wrong? I have had similar code working in a static class.





Aucun commentaire:

Enregistrer un commentaire