Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
I'm trying to use GetValue from PropertyInfo but keep getting "Object Does Not match target type". I have tried using a string variable and it says Me is not recongized which I saw in other help.

VB
For Each logentry As LogEntry In _lr
            For Each _pi In logentry.GetType.GetProperties()

                Dim _val = _pi.GetValue(_pi.GetType(), Nothing)
                Console.WriteLine(String.Format("{0}:  {1}", _pi.Name, _val))
            Next
        Next
Posted

1 solution

You are trying to use this method: http://msdn.microsoft.com/en-us/library/b05d59ty%28v=vs.110%29.aspx[^].

You do it completely wrong, with wrong parameters. First parameter should be some instance of the object. It's totally pointless to get _pi.GetType, this type is known, PropertyInfo. Second parameter will be non-null only for indexed properties.

All you need is to think a bit. You wrote you code without any understanding of what object would you need to get and from what object. This is easy, just think about it. I don't know where is the object to get a property value, because you started with its type, you may or may not have it. The first parameter can be null, quite naturally, that means this is a static property. You can sort out static properties from non-static using appropriate BindingFlags.

—SA
 
Share this answer
 
Comments
Member 4645801 2-May-16 12:53pm    
What a Jerk
Ron Anders 27-May-22 8:48am    
Yeah.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900