Это действительно зависит от объекта, который вы пытаетесь сравнить, но при этом будут сравниваться классы, у которых есть только дети (без внуков?). Он использует отражение, чтобы получить все свойства в классе и сравнить их.
Private Function Compare(ByVal Obj1 As Object, ByVal Obj2 As Object) As Boolean
'We default the return value to false
Dim ReturnValue As Boolean = False
Try
If Obj1.GetType() = Obj2.GetType() Then
'Create a property info for each of our objects
Dim PropertiesInfo1 As PropertyInfo() = Obj1.GetType().GetProperties()
Dim PropertiesInfo2 As PropertyInfo() = Obj2.GetType().GetProperties()
'loop through all of the properties in the first object and compare them to the second
For Each pi As PropertyInfo In PropertiesInfo1
Dim CheckPI As PropertyInfo
Dim CheckPI2 As PropertyInfo
Dim Value1 As New Object
Dim Value2 As New Object
'We have to do this because there are errors when iterating through lists
CheckPI = pi
'Here we pull out the property info matching the name of the 1st object
CheckPI2 = (From i As PropertyInfo In PropertiesInfo2 Where i.Name = CheckPI.Name).FirstOrDefault
'Here we get the values of the property
Value1 = CType(CheckPI.GetValue(Obj1, Nothing), Object)
Value2 = CType(CheckPI2.GetValue(Obj2, Nothing), Object)
'If the objects values don't match, it return false
If Object.Equals(Value1, Value2) = False Then
ReturnValue = False
Exit Try
End If
Next
'We passed all of the checks! Great Success!
ReturnValue = True
End If
Catch ex As Exception
HandleException(ex)
End Try
Return ReturnValue
End Function