简体   繁体   English

C#如何从组合框中的类型列表中的给定反射类型创建实例

[英]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. 我正在尝试创建组合框所选项目的实例,该实例是称为BotPlayer的类的子类,并且打算利用该类及其所有子类中存在的名为“ Move”的方法。 I'm also trying to pass that instance into a BotPlayer variable called Bot. 我还试图将该实例传递到名为Bot的BotPlayer变量中。 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. 我尝试了使用Activator.CreateInstance的不同方式,但是它似乎对我不起作用,或者我对它的理解不足以将其实现到自己的程序中。 This was the furthest I was able to get 这是我能得到的最远的

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

When I run my program it gives me this error: "System.MissingMethodException: 'No parameterless constructor defined for this object.'" 当我运行程序时,它给了我这个错误:“ System.MissingMethodException:'没有为此对象定义无参数构造函数。'”

This is the code for the combo box which exists in the Designer.cs 这是Designer.cs中存在的组合框的代码

            this.Difficulty.FormattingEnabled = true;
        this.Difficulty.Items.AddRange(BotNames.ToArray());
        this.Difficulty.Location = new System.Drawing.Point(205, 181);
        this.Difficulty.Name = "Difficulty";
        this.Difficulty.Size = new System.Drawing.Size(137, 21);
        this.Difficulty.TabIndex = 3;

This is the code for the combo box which exists in the normal cs file 这是普通cs文件中存在的组合框的代码

        if (Difficulty.SelectedItem != null)
        {
            Bot = (BotPlayer)Difficulty.SelectedItem;    //This is called casting
            Bot.Type = BotType;
            //Bot = (BotPlayer)Activator.CreateInstance((Type)Difficulty.SelectedItem);
            //Bot = (BotPlayer)Activator.CreateInstance("MyAssembly", "BotPlayer");
        }

This is the BotPlayer Constructor 这是BotPlayer构造函数

       public BotPlayer(GameBoard board, SquareValues type)
    {
        Type = type;
        Board = board;
       // Difficuty = difficulty;
    }

This is the constructor for all its subclasses 这是其所有子类的构造函数

    public BotPlayer1(GameBoard board, SquareValues type) : base(board, type)
    {
        Board = board;
        Type = type;
        BotName = "Level 1";          
    }

The only difference between the subclasses is the number at the end of BotPlayer and the bot name which is equivalent to that number with the word "Level" behind it 子类之间的唯一区别是BotPlayer末尾的数字和bot名称,该名称与该名称后面带有“ Level”字样的数字等效

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

I found that all I needed to do was pass in parameters into the Activator.CreateInstance. 我发现我需要做的就是将参数传递到Activator.CreateInstance中。

Thanks to @elgonzo and @Neil for helping me realize this. 感谢@elgonzo和@Neil帮助我实现了这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM