【问题标题】:PropertyInfo.GetValue() "Object does not match target type."PropertyInfo.GetValue() "对象与目标类型不匹配。"
【发布时间】:2008-11-21 23:10:55
【问题描述】:

我第一次深入研究反射,我真的被困住了。我用谷歌搜索了我能想到的一切。我现在想达到 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 到死,然后最后一次寻找乐趣;)

【问题讨论】:

    标签: vb.net reflection propertyinfo


    【解决方案1】:

    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)
    

    【讨论】:

    • 我刚醒来,试了一下,它就像一个魅力!我认为 GetValue 方法的第一个参数是指您要从中检索值的 PropertyInfo 对象。再次感谢!
    • +1 这对我也有用。我的情况不同;我使用的是 propInfo.GetValue(It),但属性信息取自错误的类。干杯老兄。
    • +1 我在使用 GetValue 方法时遇到了麻烦。您对确保使用构建 PropertyInfo 的对象调用 GetValue 的解释是正确的!
    【解决方案2】:

    我不太确定我知道你想在这里做什么,但我会试一试。

    这是我想出的代码:

    调用

            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
    

    【讨论】:

      猜你喜欢
      • 2022-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多