新代码
string jsonObjects = @"{ 'attrs':[ { 'name':'_DB_ATTR_OSD_PARENT_', 'column':'OsDeployerParent', 'type':'Integer', 'enumName':null }, { 'name':'_DB_ATTR_SMALLICON_', 'column':'CurrentSmallIcon', 'type':'Enum' } ] }";
string jsonObjects2 = @"{ 'attrs':[ { 'name':'_DB_ATTR_OSD_PARENT_', 'column':'OsDeployerParent1', 'type':'Integer', 'enumName':null }, { 'name':'_DB_ATTR_SMALLICON_', 'column':'CurrentSmallIcon', 'type':'Enum' } ] }";
System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
var objects = js.Deserialize(jsonObjects, typeof(testObjects));
var objects2 = js.Deserialize(jsonObjects2, typeof(testObjects));
ObjectComparer2 cmp = new ObjectComparer2();
testObjects deserializedObject = ((testObjects)objects);
testObjects deserializedObject2 = ((testObjects)objects2);
bool isObjectEquals = cmp.Equals(deserializedObject, deserializedObject2);
Console.WriteLine(string.Format("Objects are {0}", isObjectEquals ? "Equals" : "Not Equals"));
Class ObjectComparer
Inherits EqualityComparer(Of testObject)
Public Overrides Function Equals(c1 As testObject, c2 As testObject) As Boolean
If c1.name = c2.name AndAlso c1.column = c2.column AndAlso c1.enumName = c2.enumName AndAlso c1.type = c2.type Then
Return True
Else
Return False
End If
End Function
Public Overrides Function GetHashCode(c As testObject) As Integer
Dim hash As Integer = 23
If c.name IsNot Nothing Then
hash = hash * 37 + c.name.GetHashCode()
End If
If c.column IsNot Nothing Then
hash = hash * 37 + c.column.GetHashCode()
End If
If c.enumName IsNot Nothing Then
hash = hash * 37 + c.enumName.GetHashCode()
End If
If c.type IsNot Nothing Then
hash = hash * 37 + c.type.GetHashCode()
End If
Return hash
End Function
结束类
类 ObjectComparer2 继承 EqualityComparer(Of testObjects) 公共覆盖函数 Equals(c1 As testObjects, c2 As testObjects) As Boolean Dim cmp As New ObjectComparer()
For Each obj1 As testObject In c1.attrs
If Not c2.attrs.Any(Function(x) cmp.Equals(x, obj1)) Then
Return False
End If
Next
Return True
End Function
Public Overrides Function GetHashCode(c As testObjects) As Integer
Dim hash As Integer = 23
For Each obj As testObject In c.attrs
hash += obj.GetHashCode()
Next
Return hash
End Function
结束类