小编Dav*_*y C的帖子

将泛型<TObject>类传递给表单

我似乎无法通过搜索找到答案,所以这里......

我知道我可以通过利用这种类型的代码将Class对象一般传递给其他类:

public class ClsGeneric<TObject> where TObject : class
{
    public TObject GenericType { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

然后以这种方式构建:

ClsGeneric<MyType> someName = new ClsGeneric<MyType>()
Run Code Online (Sandbox Code Playgroud)

但是,我有一个应用程序,要求我打开一个表单,并以某种方式传递泛型类型以便在该表单中使用.我试图能够为许多不同的类类型重用此表单.

有谁知道这是否可能,如果可能,如何?

我已经使用Form构造函数进行了一些实验,但无济于事.

非常感谢,戴夫

更新:澄清我想要达到的结果是什么

在此输入图像描述

更新:今年4月4日,我进一步前进,但我提供了解决方案的赏金.这就是我现在拥有的:

interface IFormInterface
{
    DialogResult ShowDialog();
}


public class FormInterface<TObject> : SubForm, IFormInterface where TObject : class
{ }

public partial class Form1 : Form
{
    private FormController<Parent> _formController;

    public Form1()
    {
        InitializeComponent();
            _formController = new FormController<Parent>(this.btnShowSubForm, new DataController<Parent>(new MeContext()));   
    }
}

public class FormController<TObject> where TObject : class
{
    private …
Run Code Online (Sandbox Code Playgroud)

c# forms generic-programming tobject

10
推荐指数
2
解决办法
1188
查看次数

标签 统计

c# ×1

forms ×1

generic-programming ×1

tobject ×1