.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

Get different properties from objects

Posted By: Mahesh     Posted Date: March 15, 2011    Points:5   Category :ASP.Net
i am having two objects which are of same type, they may contains child objects, i need to find the properties from the objects which are having different values, how it is to be achived...?

Author: Mahesh             
Posted Date: March 17, 2011     Points: 20   

hi friends i got the answer here is the simple code which uses reflection, for this if your objects contains child objects/ or child classes then you should know the root namspace for the child objects

Private Sub GetModifiedProperties(ByRef aoObjectOriginal As Object, ByRef aoObjectModified As _
Object, ByRef aoObjectUpdated As Object, Byval asBaseClassNamspace as String)
Dim sOriginatValue As String
Dim sModifiedValue As String
Dim oModifiedPropertyInfo As Reflection.PropertyInfo
Dim oUpdatedPropertyInfo As Reflection.PropertyInfo

For Each oItems As Reflection.PropertyInfo In aoObjectOriginal.GetType().GetProperties()

If oItems.GetValue(aoObjectOriginal, Nothing) IsNot Nothing Then
If oItems.PropertyType.FullName.StartsWith(asBaseClassNamspace) Then ' e.g. asBaseClassNamspace = "System."

'Recersive call to the function to find properties of child objects from child classes
GetModifiedProperties(oItems.GetValue(aoObjectOriginal, Nothing), _
GetObject(aoObjectModified, oItems.Name, asBaseClassNamspace ), _
GetObject(aoObjectUpdated, oItems.Name, asBaseClassNamspace ), asBaseClassNamspace)
oModifiedPropertyInfo = aoObjectModified.GetType.GetProperty(oItems.Name)
oUpdatedPropertyInfo = aoObjectUpdated.GetType.GetProperty(oItems.Name)

sOriginatValue = Convert.ToString(oItems.GetValue(aoObjectOriginal, Nothing))
sModifiedValue = Convert.ToString(oModifiedPropertyInfo.GetValue(aoObjectModified, Nothing))

If sOriginatValue.ToLower <> sModifiedValue.ToLower Then
oUpdatedPropertyInfo.SetValue(aoObjectUpdated, sModifiedValue, Nothing)
End If
End If
End If

End Sub

Private Function GetObject(ByRef aoObjectToFound As Object, ByVal asObjectName As String, Byval asBaseClassNamspace as String) As Object
For Each oItems As Reflection.PropertyInfo In aoObjectToFound.GetType().GetProperties()
If oItems.GetValue(aoObjectToFound, Nothing) IsNot Nothing Then
If oItems.PropertyType.FullName.StartsWithasBaseClassNamspace) Then
If oItems.Name = asObjectName Then
Return oItems.GetValue(aoObjectToFound, Nothing, asBaseClassNamspace)
Exit Function
GetObject(oItems.GetValue(aoObjectToFound, Nothing), asObjectName)
End If
End If
End If

Return Nothing
End Function

Post Reply

You must Sign In To post reply
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend