iteract through a class properties

vagueante

Active member
Joined
Feb 20, 2009
Messages
28
Location
Portugal
Programming Experience
10+
Whats the best way to iteract through a class to get all the properties names?
So far i have this code


VB.NET:
Dim classInstance As New ServiceReference1.draftClaimEntryDefinition
        For Each PropertyItem As PropertyInfo In classInstance.GetType().GetProperties()
            Dim strPropName As String = String.Empty
            strPropName = PropertyItem.Name
        Next

My problem is: I will receive a xml file, and have to get the data in a class, but as mentioned before the class has a lot of classes as properties, and i want do do it without hardcode, I want to loop the class properties names and compare them to the xml tags (it haves the same name as the class properties), and put the values from the xml file in an instance object of the class.

Any help?
 
I think you mean "iterate", not "iteract".

As for the question, what's the problem? The code you have is iterating through all the properties of the object and getting their names. That's what you asked for and you already have it, so I can only assume that what you actually want is something other than what you asked for.
 
I think you mean "iterate", not "iteract".

As for the question, what's the problem? The code you have is iterating through all the properties of the object and getting their names. That's what you asked for and you already have it, so I can only assume that what you actually want is something other than what you asked for.

You're right : iterate.

My code only shows "first level" of the class, but some properties are also classes, which have properties bellow

VB.NET:
 Partial Public Class claimMarket
        Inherits Object
        Implements System.ComponentModel.INotifyPropertyChanged
        
        Private claimTypeField As String
        
        
        Private claimMarketVehicleField As claimMarketVehicle ' This is another class
......

Partial Public Class claimMarketVehicle
        Inherits Object
        Implements System.ComponentModel.INotifyPropertyChanged
        
        Private vehicleIdentificationField As VINID  ' This is another class
        
        Private vehicleDeliveryDateField As Date


what i want is to get all properties name so i can compare with the xml tags that i receive, no matter where they are in the class.

Hope i get to explain what i need.

thanks
 
That will require the use of recursion. You pass an object to a method and get all its property values and then, for each of those objects, you'd call that same method again. That will allow you to go as deep as you like into the composition tree. You'll just have to decide on the appropriate conditions to NOT make a recursive call.
 
Back
Top