in Education by
I'm working on migrating/rewriting some Java generics in C#. I'm getting an error that I don't understand. (This is partially an experiment based on composition over inheritance to limit bloat for child-classes that don't need some functionality, but it's also just an experiment to better understand C# generics.) Note: the actual child-class implementations actually work like I expect, it's just the extension method that isn't compiling. A parent class: public abstract class PageObject where T : PageObject { protected IWebDriver WebDriver => ServiceLocator.GetWebDriver(); public PageObject() { PageFactory.InitElements(WebDriver, this); } // ... more stuff, but the constructor is the important thing that keeps // me using an abstract parent class. There are some abstract methods // that return T so the child classes can return "this" in a fluent api. } An interface: public interface IHasCustomLoadCondition where T : PageObject { bool IsLoaded(); } and an extension method, this is where the error is occuring: public static T WaitForCustomPageLoad(this T page) where T : IHasCustomLoadCondition { wait.Until((d) => { return page.IsLoaded(); }); return page; } The error message: The type 'T' cannot be used as type parameter 'T' in the generic type or method 'IHasCustomLoadCondition'. There is no boxing conversion or type parameter conversion from 'T' to 'PageObject'. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Since you have this declaration: public interface IHasCustomLoadCondition where T : PageObject ^---------------------^ You must ensure you also carry this constraint into derived interfaces, implementing classes and methods that are also generic on the same T, so this method: public static T WaitForCustomPageLoad(this T page) where T : IHasCustomLoadCondition Must also have this constraint: public static T WaitForCustomPageLoad(this T page) where T : PageObject, IHasCustomLoadCondition Basically: public static T WaitForCustomPageLoad(this T page) where T : IHasCustomLoadCondition ^ ^ | | +-------- constraints must be compatible ---------+

Related questions

0 votes
    I'm working on migrating/rewriting some Java generics in C#. I'm getting an error that I don't ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 26, 2022 in Education by JackTerrance
0 votes
    I'm working on migrating/rewriting some Java generics in C#. I'm getting an error that I don't ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I'm creating a very basic AI with Tensorflow, and am using the code from the official docs/tutorial. Here's my ... Tensorflow 1.13.1. Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    What is Boxing and Unboxing in C#?...
asked Jul 9, 2021 in General by JackTerrance
0 votes
    What is the difference between boxing and unboxing in C#?...
asked Mar 31, 2021 in Education by JackTerrance
0 votes
    F# allows constraining generic types on the type's members, similar to: type ClassWithMemberConstraint = class end ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    I have a generic method defined like this: public void MyMethod(T myArgument) The first thing I want to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    I have a generic method defined like this: public void MyMethod(T myArgument) The first thing I want to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    I have a generic method defined like this: public void MyMethod(T myArgument) The first thing I want to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    I have a generic method defined like this: public void MyMethod(T myArgument) The first thing I want to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    What are extension methods in C#?...
asked Mar 31, 2021 in Education by JackTerrance
0 votes
    I have a generic method which a generic type parameter T which is a subclass of MyClass. Inside that method, I want ... of type erasure): object Demo extends App { def myMethod[T...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    I'm trying to write generic method to cast types. I want write something like Cast.To(variable) instead ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    I'm trying to write generic method to cast types. I want write something like Cast.To(variable) instead ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    Which of these is an correct way of defining generic method? (a) name(T1, T2, , Tn) { /* */ } ... topic in portion Generics of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
...