Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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.

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

like image 556
StevenMcD Avatar asked Nov 21 '08 23:11

StevenMcD


People also ask

How do I retrieve the value of a non-indexed property?

You call the GetValue (Object) overload to retrieve the value of a non-indexed property; if you try to retrieve the value of an indexed property, the method throws a TargetParameterCountException exception. You can determine whether a property is indexed or not by calling the GetIndexParameters method.

What does getValue return when overridden in a derived class?

When overridden in a derived class, returns the property value of a specified object that has the specified binding, index, and culture-specific information. Returns the property value of a specified object. public object? GetValue (object? obj); The object whose property value will be returned. The property value of the specified object.

What is the use of getValue () method?

Returns the property value of a specified object with optional index values for indexed properties. When overridden in a derived class, returns the property value of a specified object that has the specified binding, index, and culture-specific information. Returns the property value of a specified object. public object? GetValue (object? obj);

What are the properties of an object?

The property value of the specified object. The following example defines a Planet class that has two properties: Name, the name of the planet; and Distance, the planet's distance from Earth.


1 Answers

Me refers to the ObjectCompare object, which is different than the class from which the PropertyInfo objects were derived (Class2). You need to also pass in an object of the type from which you retrieved the PropertyInfo objects.

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)
like image 146
tvanfosson Avatar answered Nov 19 '22 20:11

tvanfosson