PropertyInfo.GetValue()“对象与目标类型不匹配。”

时间:2008-11-21 23:10:56

标签: vb.net reflection propertyinfo

我第一次深入反思,我真的陷入困境。我用谷歌搜索了我能想到的一切。我现在想成为90%。

我正在尝试通过Reflection返回自定义类中的Property的值。

这是我的班级声明:

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和谷歌,然后在上次有趣的时候;)

2 个答案:

答案 0 :(得分:20)

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)

答案 1 :(得分: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