vendredi 2 novembre 2018

C# How to create an instance from a given reflection Type from a list of Type in a Combo-Box

List<Type> BotNames = typeof(BotPlayer).Assembly.GetTypes().Where(type => type.IsSubclassOf(typeof(BotPlayer))).ToList();

I've put that list into a combo box to be displayed to the user in the drop-down menu. I'm trying to create an instance of the selected item of the combo box which is a subclass of a class called BotPlayer and is meant to make use of a method called "Move" which is present in the class and all its subclasses. I'm also trying to pass that instance into a BotPlayer variable called Bot. I've tried the different ways of using Activator.CreateInstance but it doesn't seem to work for me or I don't understand it enough to implement it into my own program. This was the furthest I was able to get

Bot = (BotPlayer)Activator.CreateInstance((Type)Difficulty.SelectedItem);





Aucun commentaire:

Enregistrer un commentaire