Dynamic access of an object’s properties

Lets say you have an object, and you want to dynamic access the object’s properties, what you do?

Use ‘Reflection’…here a not so hypothetical scenario:

Suppose your ORM tool have map a database table to the following class :

public partial class TheoriticalLessonsParticipant
    {
        public long ListId { get; set; }
        public string Participant1 { get; set; }
        public string Participant2 { get; set; }
        public string Participant3 { get; set; }
        public string Participant4 { get; set; }
        public string Participant5 { get; set; }
        public string Participant6 { get; set; }
        public string Participant7 { get; set; }
        public string Participant8 { get; set; }
        public string Participant9 { get; set; }
        public string Participant10 { get; set; }
        public string Participant11 { get; set; }
        public string Participant12 { get; set; }
        public string Participant13 { get; set; }
        public string Participant14 { get; set; }
        public string Participant15 { get; set; }
        public string Participant16 { get; set; }
    }
}

ok, pretty strain forward until this point, know lets suppose that you have a user interface that allows the user to fulfill the participants counting from 1 to 16, without you having the possibility to know how many he will put.

How are you going to update the correlating properties and carry on with whatever else you want to do??

Well, if you use an array to hold the participants that the user inputs you can count them, but then what? how will you iterate throughout the properties and update them ? You can write something like this ( supposing that the user input is an array of : String[] UserInput ):

if ( UserInput.Lenght ==1)

{

TheoriticalLessonsParticipantObject.Participant1 = UserInput[0] ;

}elseif ( UserInput.Lenght ==2)

{

TheoriticalLessonsParticipantObject.Participant1 = UserInput[0] ;

TheoriticalLessonsParticipantObject.Participant2 = UserInput[1] ;

}elseif ( … )

.

.

.

else

{

TheoriticalLessonsParticipantObject.Participant1 = UserInput[0] ;

TheoriticalLessonsParticipantObject.Participant2 = UserInput[1] ;

TheoriticalLessonsParticipantObject.Participant16 = UserInput[15] ;

}

and so on….( god help us! ) OR you could use Reflection on the object’s properties to access them dynamically at run-time. Here is how:

1:  Type tempRefl 
         = TheoriticalLessonsParticipantObject.GetType();

2:  for (int y = 0; y < UserInput.Length; y++)
    {
3:   string tempProp = "Participant" + (y + 1).ToString();
4:   PropertyInfo tempInfo = tempRefl.GetProperty(tempProp);
5:   tempInfo.SetValue(tempPartList, UserInput[y].ToString()
                                                 , null);
    }

at line 1 you are getting the type of the object you want to dynamically access the properties,

at line 2 you are iterating throughout the user’s input values,

at line 3 you are creating a string that holds at each iteration the property name, in our case our properties are “Participant1″,”Participant2”, “Participant3″….”Participant16”

at line 4 you are actually getting the property that you want ,passing it’s name which is dynamically created at line 3, at the .GetProperty() function,

at line 5, you are using the function .SetValue() to set the value to the relevant property.

That’s all, the above piece of code will actually count the participants that the user enters at the array, and update only the relevant object’s properties.

Leave a comment