将 BindingSource.DataSource 声明为通用

Declare BindingSource.DataSource as generic

我正在尝试将 bindingSource 声明为泛型控件中的泛型

public partial class ABMControl<T> : UserControl
{
    public ABMControl()
    {
        InitializeComponent();
    }
}
partial class ABMControl<T>
{
    ...
    private void InitializeComponent()
    {
        ...
        this.bindingSource.DataSource = typeof(T)
        ...
    }
    ...
}

但在设计器中这是问题所在:

Failed to parse method 'InitializeComponent'. The parser reported the following error 'Type parameters are not suppported Parameter name: typeSymbol'. Please look in the Task List for potential errors.

为防止设计者出错,在构造函数中设置绑定源的数据源。

当您将一段代码放入控件设计器的构造函数中时,解串器将不会尝试解析它。它也不会 运行 在您的控件的设计时,而在 run-time 和派生控件中,它会 运行.

以下是防止错误的必要条件:

public partial class ABMControl<T> : UserControl
{
    public ABMControl()
    {
        InitializeComponent();
        this.bindingSource.DataSource = typeof(T)
    }
}

有关设计器如何工作的更多信息,请查看以下内容post,特别查看包含一些错误但显示设计器的示例: