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


Top 5 Contributors of the Month
satyapriyanayak

Home >> Code Snippets >> Serilization >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Using Reflection to iterate properties of object at runtime

Posted By:Mahesh       Posted Date: March 17, 2011    Points: 40    Category: Serilization    URL: http://www.dotnetspark.com  

Reflection to iterate properties of object at runtime
 

This is the method which is useful in the situation where you do not know about properties of objects.

Suppose you are having a class which contains many properties and also contains sub classes, now suppose at the runtime if you create an onject of that class and now you wants to iterate each properties of that object dinamically then this method will be very useful for you.

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
Else
GetObject(oItems.GetValue(aoObjectToFound, Nothing), asObjectName)
End If
End If
End If
Next

Return Nothing
End Function


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

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