【问题标题】:Test If Property of type System.Collections.Generic.List(of T)测试 System.Collections.Generic.List(of T) 类型的属性
【发布时间】:2012-04-22 14:57:04
【问题描述】:

我需要能够判断一个属性是否属于 List(of T) 但我目前无法。如果我这样做了

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

我收到以下错误

TypeOf (UpdateTo.GetType.GetProperty(node.Name)) 是 System.Collections.Generic.List(Of Object) 类型的表达式 'System.Reflection.PropertyInfo' 永远不能是类型 '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

【问题讨论】:

    标签: .net vb.net list types


    【解决方案1】:

    首先,UpdateTo.GetType.GetProperty(node.Name).GetType() 总是返回 PropertyInfo。我猜你的意思是UpdateTo.GetType.GetProperty(node.Name).GetPropertyType()

    其次,List(of T) 不是类型。 List(of String) 是一种类型,但不是 List(of Object) 的子类。没有简单的方法可以知道给定类型是否是某种List(of T)。也许你应该简单地测试一下属性的类型是否实现了IList

    【讨论】:

    • 如果它实现了 ilist,我该如何进行测试?
    • 也与“GetPropertyType()”有关...我收到错误消息“GetPropertyType”不是“System.Reflection.PropertyInfo”的成员。
    • 抱歉,请将 .GetPropertyType() 替换为 .PropertyType。至于测试是否实现IList,使用方法Type.IsSubclassOf(Type)
    • AFAIK,Type.IsSubclassOf(Type) 不告诉是否实现了接口。相反,可以使用 typeof(IList).IsAssignableFrom(prop.GetType()) 其中 prop 是属性的类型。
    猜你喜欢
    • 2021-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-30
    • 2012-07-04
    • 2015-11-18
    • 2021-08-26
    • 1970-01-01
    相关资源
    最近更新 更多