我正在尝试使用反射来比较两个对象,以循环遍历其所有属性。但是,当我尝试比较SortedList时,我会陷入困境:
代码
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`2 [System.String,ANAPLMVC.MyClass]'类型的对象强制转换为'System.Collections.SortedList'。
为了能够将这些对象转换为SortedLists,我需要做什么才能比较它们?
答案 0 :(得分:0)
System.Collections.Generic.SortedList
以任何方式与System.Collections.SortedList
无关。演员表演是不可能的。请考虑转换为其中一个接口,这些接口由通用SortedList
:
System.Collections.IEnumerable
System.Collections.ICollection
System.Collections.IDictionary