7

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

リフレクションを介してカスタム クラスのプロパティの値を返そうとしています。

これが私のクラス宣言です:

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を死ぬほど検索し、最後に楽しみのために検索しました;)

4

2 に答える 2

21

Meは、オブジェクトの派生元ObjectCompareのクラス ( ) とは異なるオブジェクトを参照します。オブジェクトを取得したタイプのオブジェクトも渡す必要があります。PropertyInfoClass2PropertyInfo

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)
于 2008-11-22T00:41:45.477 に答える
1

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

これが私が思いついたコードです:

呼び出し:

        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
于 2008-11-22T00:49:35.633 に答える