Search code examples
c#winformsgenericsuser-controlswindows-forms-designer

Declare BindingSource.DataSource as generic


i'm trying to declare bindingSource as generic in a control generic

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

But in the designer this is the problem:

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.

But in the designer this is the problem


Solution

  • To prevent designer error, set data source of binding source in constructor.

    When you put a piece of code in constructor of your control designer deserializer will not try to parse it. It also will not run in design time of your control, while in run-time and also for derived control, it will run.

    Here is what you should have to prevent error:

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

    For more information about how designer works take a look at the following post, specially take a look at the example which contains a couple of errors but shows the designer: