首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用反射将[System.Collections.Generic.SortedList`2]转换为SortedList

使用反射将[System.Collections.Generic.SortedList`2]转换为SortedList
EN

Stack Overflow用户
提问于 2013-09-10 19:09:46
回答 1查看 428关注 0票数 0

我试图比较两个对象之间的关系,使用反射遍历它们的所有属性。然而,当我试图比较一个SortedList时,我被困住了:

代码语言:javascript
复制
Private Sub CompareObjects(obj1 As Object, obj2 As Object)

            Dim objType1 As Type = obj1.GetType()

            Dim propertyInfo = objType1.GetProperties

            For Each prop As PropertyInfo In propertyInfo
                Dim paramInfo = prop.GetIndexParameters
                If paramInfo.Count > 0 Then Continue For
                If Not prop.CanWrite Then Continue For

                If GetType(SortedList).IsAssignableFrom(prop.PropertyType) OrElse _
                    prop.PropertyType.Name.ToString.Equals("SortedList`2") Then
                    Dim itemList1 As SortedList = DirectCast(prop.GetValue(obj1), SortedList)
                    Dim itemList2 As SortedList = DirectCast(prop.GetValue(obj2), SortedList)

错误消息(来自Dim itemList1 As SortedList = DirectCast(prop.GetValue(obj1),SortedList))

无法将'System.Collections.Generic.SortedList`2System.String,ANAPLMVC.MyClass‘类型的对象强制转换为“System.Collection.System.Collections.SortedList”。

我需要做些什么才能将这些对象转换成SortedLists以便我可以比较它们呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-10 19:12:35

System.Collections.Generic.SortedListSystem.Collections.SortedList没有任何关系。那个演员是不可能的。考虑转换到这些接口之一,这些接口由泛型SortedList实现。

  • System.Collections.IEnumerable
  • System.Collections.ICollection
  • System.Collections.IDictionary
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18727005

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档