简体   繁体   English

返回参数中分配的类型的变量?

[英]Return a variable of type assigned in parameters?

I'm trying to create a class like Windows Form, which will have multiple new features. 我正在尝试创建类似Windows Form的类,它将具有多个新功能。 It's simply going to be a "better" version of Form , making my work easier on this exact program. 它将仅仅是Form的“更好”版本,从而使我在此精确程序上的工作更加轻松。 Here's what I have so far: 这是我到目前为止的内容:

public class SuperForm : Form
{
    protected abstract void OnSizeChanged(object sender, EventArgs e);

    public SuperForm()
    {
        this.SizeChanged += OnSizeChanged;
    }
}

Not much, it only makes sure every Form will have to define OnSizeChanged which will be called automatically when size changes, but there's going to be more. 不多,它只是确保每个Form都必须定义OnSizeChanged ,当大小更改时会自动调用它,但是还会有更多。

What I need next is a method which takes a class/type as it's parameter and initializes a new object of that type, automatically adds it to Controls and then returns the object. 接下来,我需要一个方法,该方法将类/类型作为参数,并初始化该类型的新对象,然后将其自动添加到Controls ,然后返回该对象。 Here's an example code (not working) on how I would like it to be like: 这是一个示例代码(不起作用),说明我希望如何:

public cls NewControl(ClassType cls) // This obviously doesn't work.
// I need it to return an object of type "cls" parameter.
{
    cls newControl = new cls();
    this.Controls.Add(newControl);
    return newControl;
}

I know ClassType is not a valid type, that's why I need help. 我知道ClassType不是有效的类型,这就是为什么我需要帮助。

And then I could basically call it like this: 然后我基本上可以这样称呼它:

Button b = this.NewControl(Button);

Which would return a new button and also add it to this.Controls . 这将返回一个新按钮,并将其添加到this.Controls I might eventually need to do more of these common tasks when a control is initialized, so that's why I'd like to have it in it's own method. 初始化控件后,我可能最终需要做更多的这些常见任务,所以这就是为什么我想将其包含在自己的方法中。

Is this possible in C#? 这在C#中可能吗? If not, are there any workarounds? 如果没有,是否有任何解决方法? One way would be to define a method for each class inheriting from Control like this: 一种方法是为每个继承自Control类定义一个方法,如下所示:

public Button NewControl(Button b);
public TextBox NewControl(TextBox tb);
public ListBox NewControl(ListBox lb);

But it doesn't seem like a valid option to me. 但这对我来说似乎不是一个有效的选择。

It sounds like you want to make a generic method, with a couple of constraints on the type parameter: 听起来您想创建一个通用方法,并且对type参数有一些约束:

public T CreateAndAdd<T>() where T : Control, new()
{
    T newControl = new T();
    Controls.Add(newControl);
    return newControl;
}

Here the T : Control constraint makes sure that you're creating a control, so that you'll be able to use Controls.Add . 在这里, T : Control约束确保您正在创建控件,以便能够使用Controls.Add The T : new() constraint makes sure the type argument has a public parameterless constructor, so that you can call new T() . T : new()约束确保类型参数具有公共的无参数构造函数,以便您可以调用new T()

To create a TextBox, for example, you would call the function like this: 例如,要创建一个TextBox,您可以这样调用函数:

var tb = CreateAndAdd<TextBox>()

(I've renamed the method for what I believe to be clarity, btw.) (顺便说一句,我为清楚起见,已将方法重命名。)

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

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