I have the following classes/interfaces:
public abstract class AbstractBasePresenter<T> : IPresenter<T>
where T : class, IView
{
}
public interface IPresenter<T>
{
}
public interface IView<TV, TE, TP> : IView
where TV : IViewModel
where TE : IEditModel
//where TP : AbstractBasePresenter<???>
{
}
public interface IView {}
Is there any way that I can constrain TP on IView<> to be a class that inherits from AbstractBasePresenter?
Or is my only alternative to create a non-generic IPresenter interface and then update IPresenter to implement it and then use check “TP : IPresenter”?
Thanks
Update:
Proposed answer below does not work:
public interface IView<TV, TE, TP> : IView
where TV : IViewModel
where TE : IEditModel
where TP : AbstractBasePresenter<IView<TV,TE,TP>>
{
}
I have interface declared as:
public interface IInsuredDetailsView : IView<InsuredDetailsViewModel, InsuredDetailsEditModel, IInsuredDetailsPresenter>
{ }
public interface IInsuredDetailsPresenter : IPresenter<IInsuredDetailsView>
{ }
Compiler complains that IInsuredDetailsPresenter is not assignable to AbstractBasePresenter>
You can do it, but you need to provide one more type argument to the
IView<>interface:Edit:
According to editions in your question:
IInsuredDetailsPresenteris definitely not assignable toAbstractBasePresenter. Compiler is complaining due to the constraint you requested in your original question. More specifically due to this partIt seems you want to constrain
TPto be an interface as well. You may try the below piece of code:Constraints on
Tare no more needed, becauseIPresenter<T>has none. Of course, you could adapt armen.shimoon’s answer in a similar manner. The point is to replaceAbstractBasePresenter<T>constraint withIPresenter<T>constraint.