PropertyInfo.GetValue()“オブジェクトがターゲットタイプと一致しません。”

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

  •  10-07-2019
  •  | 
  •  

質問

私は初めてReflectionを掘り下げて、本当に行き詰まっています。私が考えることができるすべてをグーグルで調べました。今は90%の場所にいます。

Reflectionを介してカスタムクラスのプロパティの値を返そうとしています。

ここに私のクラス宣言があります:

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

リフレクションを通してクラスを見るために書いたクラスは次のようになります:

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

property_value = propInfo.GetValue(Me、Nothing)行にブレークポイントを配置して、結果を確認します。

コードの呼び出し方法:

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()

リフレクションを介してPropertyNameとTypeを確認できますが、必要なのはPropertyの値だけです!ブレークポイントに到達すると、TargetExceptionが発生し、「オブジェクトがターゲットタイプと一致しません」というエラーメッセージが表示されます。今朝の午前1時、私は大破してしまいました。今の助けをいただければ幸いです。 MSDNとGoogleを検索して死に、最後に楽しみに検索しました;)

役に立ちましたか?

解決

Me ObjectCompare オブジェクトを参照します。これは、 PropertyInfo オブジェクトの派生元のクラス( Class2 )。 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)

他のヒント

ここで何をしようとしているのかはわかりませんが、突き刺します。

ここに私が作成したコードを示します。

通話

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


        Dim go As New ObjectCompare
        go.CompareObjects(test)

クラス

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

比較

 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
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top