C# Generic Extension Method Boxing Conversion Issue -


i'm working on migrating/rewriting java generics in c#. i'm getting error don't understand.

(this partially experiment based on composition on inheritance limit bloat child-classes don't need functionality, it's experiment better understand c# generics.)

note: actual child-class implementations work expect, it's extension method isn't compiling.

a parent class:

public abstract class pageobject<t> t : pageobject<t> {      protected iwebdriver webdriver => servicelocator.getwebdriver();      public pageobject()     {         pagefactory.initelements(webdriver, this);     }      // ... more stuff, constructor important thing keeps     // me using abstract parent class. there abstract methods     // return t child classes can return "this" in fluent api. } 

an interface:

public interface ihascustomloadcondition<t> t : pageobject<t> {     bool isloaded(); } 

and extension method, error occuring:

public static t waitforcustompageload<t>(this t page) t : ihascustomloadcondition<t> {     wait.until<bool>((d) =>     {         return page.isloaded();     });     return page; } 

the error message:

the type 't' cannot used type parameter 't' in generic type or method 'ihascustomloadcondition<t>'. there no boxing conversion or type parameter conversion 't' 'pageobject<t>'. 

since have declaration:

public interface ihascustomloadcondition<t> t : pageobject<t>                                             ^---------------------^ 

you must ensure carry constraint derived interfaces, implementing classes , methods generic on same t, method:

public static t waitforcustompageload<t>(this t page) t : ihascustomloadcondition<t> 

must have constraint:

public static t waitforcustompageload<t>(this t page)     t : pageobject<t>, ihascustomloadcondition<t> 

basically:

public static t waitforcustompageload<t>(this t page) t : ihascustomloadcondition<t>                                       ^                                                 ^                                       |                                                 |                                       +-------- constraints must compatible ---------+ 

Comments