我试图比较两个对象之间的关系,使用反射遍历它们的所有属性。然而,当我试图比较一个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`2System.String,ANAPLMVC.MyClass‘类型的对象强制转换为“System.Collection.System.Collections.SortedList”。
我需要做些什么才能将这些对象转换成SortedLists以便我可以比较它们呢?
发布于 2013-09-10 19:12:35
System.Collections.Generic.SortedList与System.Collections.SortedList没有任何关系。那个演员是不可能的。考虑转换到这些接口之一,这些接口由泛型SortedList实现。
System.Collections.IEnumerableSystem.Collections.ICollectionSystem.Collections.IDictionaryhttps://stackoverflow.com/questions/18727005
复制相似问题