c# - Passing a control as a generic parameter type and inheriting from it -


i have number of custom controls extend existing windows forms controls 1 or more interfaces designed myself. implementation of these interfaces virtually identical within each custom control, have repeating code such following:

public class customtextbox : textbox, isomeinterface {     // implementations of interface members      ... }  public class custombutton : button, isomeinterface {     // implementations of interface members      ... } 

ideally able similar following:

public abstract class basecustomcontrol<c> : c, isomeinterface c : control {     // implementations of interface members }  public class customtextbox : basecustomcontrol<textbox> {     // implementations of interface members      ... }  public class custombutton : basecustomcontrol<button> {     // implementations of interface members      ... } 

this way, identical implementations removed , consolidated single base class reduce repeating code. unfortunately, isn't possible; there suitable alternatives can use?

since c# doesn't support multiple inheritance, you're going have use composition behavior want.

define pair of interfaces; 1 "real" interface, , other nothing more provide instance of first:

public interface isomeinterface {     string foo { get; }     void bar(); }  public interface isomeinterfacecontrol {     isomeinterface someinterface { get; } } 

then create implementation of "real" interface:

public class someinterfaceimpl : isomeinterface {     private control _control;      public string foo { get; private set; }     public void bar()     {     }      public someinterfaceimpl(control control)     {         _control = control;     } } 

and modify controls implement "wrapper" interface returning instance of "real" interface implementation:

public class customtextbox : textbox, isomeinterfacecontrol {     public isomeinterface someinterface { get; private set; }      public customtextbox()     {         this.someinterface = new someinterfaceimpl(this);     } } 

now of logic contained inside "someinterfaceimpl" class, can access logic of custom controls follows:

customtextbox customtextbox = new customtextbox(); customtextbox.someinterface.bar(); 

if behavior custom controls needs vary, can introduce parallel inheritance hierarchy isomeinterface:

public class textboxsomeinterface : someinterfaceimpl {     public textboxsomeinterface(customtextbox textbox)         : base(textbox)     {     } }  public class buttomsomeinterface : someinterfaceimpl {     public buttomsomeinterface(custombutton button)         : base(button)     {     } } 

and use this:

public class customtextbox : textbox, isomeinterfacecontrol {     public isomeinterface someinterface { get; private set; }      public customtextbox()     {         this.someinterface = new textboxsomeinterface(this);     } } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -