PropertyInfo.GetValue () & # 8220; L'oggetto non corrisponde al tipo di destinazione. & # 8221;

StackOverflow https://stackoverflow.com/questions/310518

  •  10-07-2019
  •  | 
  •  

Domanda

Sto scavando in Reflection per la prima volta e sono davvero bloccato. Ho cercato su google tutto ciò che mi viene in mente. Sono il 90% dove voglio essere adesso.

Sto cercando di restituire il valore di una proprietà in una classe personalizzata tramite Reflection.

Ecco la mia dichiarazione di classe:

Public Class Class2
    Private newPropertyValue2 As String

    Public Property NewProperty2() As String
        Get
            Return newPropertyValue2
        End Get
        Set(ByVal value As String)
            newPropertyValue2 = value
        End Set
    End Property   
End Class

La classe che ho scritto per guardare la classe attraverso la riflessione si presenta così:

Public Class ObjectCompare
    Private _OriginalObject As PropertyInfo()

    Public Property OriginalObject() As PropertyInfo()
        Get
            Return _OriginalObject
        End Get
        Set(ByVal value As PropertyInfo())
            _OriginalObject = value
        End Set
    End Property

    Public Sub CompareObjects()
        Dim property_value As Object

        For i As Integer = 0 To OriginalObject.Length - 1
            If OriginalObject(i).GetIndexParameters().Length = 0 Then
                Dim propInfo As PropertyInfo = OriginalObject(i)

                Try
                    property_value = propInfo.GetValue(Me, Nothing)
                Catch ex As TargetException
                End Try   
            End If
        Next
    End Sub
End Class

Ho inserito un punto di interruzione nella riga property_value = propInfo.GetValue (Me, Nothing) per vedere qual è il risultato.

Ecco come chiamo il mio codice:

Dim test As New Class2
test.NewProperty2 = "2"

Dim go As New ObjectCompare
Dim propInf As PropertyInfo()
propInf = test.GetType.GetProperties()

go.OriginalObject = propInf

go.CompareObjects()

Attraverso la riflessione posso vedere PropertyName e Type, tutto ciò di cui ho bisogno è il valore della proprietà! Ora quando arrivo al punto di interruzione, ottengo una TargetException e il messaggio di errore dice "L'oggetto non corrisponde al tipo di destinazione." Sono le 1 del mattino e sono distrutto, qualsiasi aiuto in questo momento sarebbe apprezzato. Ho cercato MSDN e Google a morte e poi l'ultima volta per divertimento;)

È stato utile?

Soluzione

Me si riferisce all'oggetto ObjectCompare , che è diverso dalla classe da cui sono derivati ??gli oggetti PropertyInfo ( Class2 ). Devi anche passare un oggetto del tipo da cui hai recuperato gli oggetti PropertyInfo .

Public Sub CompareObjects(ByVal It as Object)
    Dim property_value As Object

    For i As Integer = 0 To OriginalObject.Length - 1
        If OriginalObject(i).GetIndexParameters().Length = 0 Then
            Dim propInfo As PropertyInfo = OriginalObject(i)

            Try
                property_value = propInfo.GetValue(It, Nothing)
            Catch ex As TargetException
            End Try   
        End If
    Next
End Sub

go.CompareObjects(test)

Altri suggerimenti

Non sono davvero sicuro di sapere cosa stai cercando di fare qui, ma ci proverò.

Ecco il codice che ho creato:

Calling :

        Dim test As New Class2
        test.NewProperty2 = "2"


        Dim go As New ObjectCompare
        go.CompareObjects(test)

Class :

Public Class Class2
    Private newPropertyValue2 As String

    Public Property NewProperty2() As String
        Get
            Return newPropertyValue2
        End Get
        Set(ByVal value As String)
            newPropertyValue2 = value
        End Set
    End Property
End Class

Confronto :

 Public Class ObjectCompare

    Public Sub CompareObjects(ByVal MyType As Object)

        For Each Prop In MyType.GetType().GetProperties()
            Dim value = Prop.GetValue(MyType, Nothing)
            Console.WriteLine(value)
        Next
        Console.ReadLine()
    End Sub
End Class
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top