How to use multiple restrictions in C# Generics properly?

Posted by plouh on Stack Overflow See other posts from Stack Overflow or by plouh
Published on 2010-03-12T09:25:52Z Indexed on 2010/03/12 9:27 UTC
Read the original article Hit count: 150

Filed under:
|

I am attempting to bind c# generics to a class and an interface like this:

public class WizardPage<T> where T : UserControl, IWizardControl 
{
    private T page;

    public WizardPage( T page ) {
        this.page = page;
    }
}

And use it with this:

public MyControl : UserControl, IWizardControl {
   //...
}

Somehow C# doesn't seem to be able to decide that MyControl is a proper instance of T as

public class Wizard<T> where T : UserControl, IWizardControl {

    private WizardPage<T> Page1;

    public Wizard( MyControl control ) {
        this.Page1 = new WizardPage(control);
    }
}

fails with error

The best overloaded method match for 'Controls.WizardPage.WizardPage(T)' has some invalid arguments

Am I doing something wrong or is this just not going to work?

© Stack Overflow or respective owner

Related posts about c#

Related posts about generics