我正在尝试使用反射来比较两个对象来遍历它们的所有属性。但是,在尝试比较 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 以便我可以比较它们?