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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/310518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:55:52  来源:igfitidea点击:

PropertyInfo.GetValue() "Object does not match target type."

vb.netreflectionpropertyinfo

提问by StevenMcD

I'm digging into Reflection for the first time and I'm truely stuck. I've googled everything I can think of. I'm 90% where I wanna be now.

我第一次深入研究反射,我真的被困住了。我已经用谷歌搜索了我能想到的一切。我现在 90% 的地方是我想去的地方。

I'm trying to return the value of a Property in a custom class through Reflection.

我正在尝试通过反射返回自定义类中的属性值。

Here's my class declaration:

这是我的类声明:

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

The class I've written to look at the class through reflection looks like this:

我编写的通过反射查看类的类如下所示:

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

I put a breakpoint on the property_value = propInfo.GetValue(Me, Nothing) line to see what the result is.

我在 property_value = propInfo.GetValue(Me, Nothing) 行上放了一个断点,看看结果是什么。

Here's how I call my code:

这是我如何调用我的代码:

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

Through reflection I can see the PropertyName and Type, all I need is the value of the Property! Now when I get to the breakpoint, I get a TargetException and the error message says "Object does not match target type." Its now 1AM in the morning and I'm wrecked, any help right now would be appreciated. I've searched MSDN and Google to death and then on last time for fun ;)

通过反射我可以看到PropertyName和Type,我需要的只是Property的值!现在,当我到达断点时,我收到 TargetException 并且错误消息显示“对象与目标类型不匹配”。现在是凌晨 1 点,我很崩溃,现在的任何帮助将不胜感激。我已经搜索了 MSDN 和 Google 到死,然后最后一次是为了好玩;)

回答by tvanfosson

Merefers to the ObjectCompareobject, which is different than the class from which the PropertyInfoobjects were derived (Class2). You need to also pass in an object of the type from which you retrieved the PropertyInfoobjects.

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)

回答by Nathan W

I'm not really sure I know what you are trying to do here but I'll have a stab at it.

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

Here is the code that I have come up:

这是我提出的代码:

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

Compare:

比较

 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