I made a list of all the subclasses of a base class using reflection using the code shown below
List<Type> BotNames = typeof(BotPlayer).Assembly.GetTypes().Where(type => type.IsSubclassOf(typeof(BotPlayer))).ToList();
Each one of these subclasses has a ToString() override which works when I make use of a normal list of instances of these subclasses but when I try to output the contents of the reflection list using the ToString() method, it uses the Default ToString() method which gives outputs such as "Checkers.Model.BotPlayer1" when its override ToString() is meant to return "Level 1". The code below is what I tested the output on( keep in mind that I am working using a GUI)
foreach (var item in BotNames)
{
Messages.Text = item.ToString();
}
If I can properly override to ToString() method, I plan to use it on the combo-box within my GUI so the contents of the reflection list are displayed with the correct names. This is where how I have the list in the Designer code in case that is required
this.Difficulty.Items.AddRange(BotNames.ToArray());
Difficulty is the name of the combo-box
Aucun commentaire:
Enregistrer un commentaire