mardi 1 avril 2014

How to get the variable with its name in C#


Vote count:

0




Want to use a variable based on its name. It is hard to describe. Here is the example



var sections = new [] {"Personnel", "General", "Medical"};

foreach (var s in sections)
{
// want to retrieve the variable "TableCellPersonnel"
(TableCell)Type.GetType(string.Format("TableCell{0}", s)).Text = "Test";
}


so that we don't have to :



TableCellPersonnel.Text="Test"
TableCellGeneral.Text="Test"
TableCellMedical.Text="Test"


So, is it possible for this kind of "reflection"?



asked 2 mins ago

Blaise

1,397





Aucun commentaire:

Enregistrer un commentaire