Steve
Steve

Reputation: 3080

Test If Property of type System.Collections.Generic.List(of T)

I need to be able to tell if a property is of type List(of T) but am currently unable to. if i do

TypeOf (UpdateTo.GetType.GetProperty(node.Name)) Is System.Collections.Generic.List(Of Object)

I get the following error

TypeOf (UpdateTo.GetType.GetProperty(node.Name)) Is System.Collections.Generic.List(Of Object) Expression of type 'System.Reflection.PropertyInfo' can never be of type 'System.Collections.Generic.List(Of Object)'.

If xmlRequestNode IsNot Nothing Then

                'if there is at least one property to update
                If xmlRequestNode.ChildNodes.Count > 0 Then

                    'loop through each property that needs setting
                    For Each node As XmlNode In xmlRequestNode.ChildNodes

                        Try

                            'TODO: We do not want to set lists of objects. Currently we have issues where it tries to set the whole object instead of 
                            'going into the object and set each property. 
                            If UpdateTo.GetType.GetProperty(node.Name).GetType() IsNot GetType(System.Collections.Generic.List(Of Object)) Then

                                'and set it to the equivalent property on the user group model that was passed in (we don't use the node value as it would need casting and could be an object)
                                UpdateTo.GetType.GetProperty(node.Name).SetValue(UpdateTo, UpdateFrom.GetType.GetProperty(node.Name).GetValue(UpdateFrom, Nothing), Nothing)

                            End If

                        Catch ex As Exception

                            'and send details of the problem
                            Return "Could not find or set a property called " & node.Name

                        End Try

                    Next

                Else

                    'and send details of the problem
                    Return "No updates were requested"

                End If

            Else

                'and send details of the problem
                Return "No object to update from was supplied"

            End If

Upvotes: 1

Views: 2425

Answers (1)

Nicolas Repiquet
Nicolas Repiquet

Reputation: 9255

First, UpdateTo.GetType.GetProperty(node.Name).GetType() always returns PropertyInfo. I guess you mean UpdateTo.GetType.GetProperty(node.Name).GetPropertyType() ?

Second, List(of T) is not a type. List(of String) is a type, but is not a subclass of List(of Object). There is no easy way to know if a given type is some kind of List(of T). Maybe you should simply test if the type of the property implements IList.

Upvotes: 2

Related Questions