Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Generic Constraint - How to refer to current class type?

Tags:

c#

generics

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>

like image 326
Rezler Avatar asked Sep 07 '12 07:09

Rezler


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is C full form?

Full form of C is “COMPILE”. One thing which was missing in C language was further added to C++ that is 'the concept of CLASSES'.


2 Answers

No problem, no need for another generic parameter:

public interface IView<TV, TE, TP> : IView
    where TV : IViewModel
    where TE : IEditModel
    where TP : AbstractBasePresenter<IView<TV,TE,TP>>
{
}

Edit: Updated question:

If you do not need the presenter to inherit from AbstractBasePresenter, change the code to:

public interface IView<TV, TE, TP> : IView
    where TV : IViewModel
    where TE : IEditModel
    where TP : IPresenter<IView<TV,TE,TP>>
{
}
like image 133
armen.shimoon Avatar answered Sep 17 '22 15:09

armen.shimoon


You can do it, but you need to provide one more type argument to the IView<> interface:

public interface IView<TV, TE, TP, T> : IView
    where TV : IViewModel
    where TE : IEditModel
    where TP : AbstractBasePresenter<T>
    where T : class, IView
{
}

Edit:

According to editions in your question: IInsuredDetailsPresenter is definitely not assignable to AbstractBasePresenter. Compiler is complaining due to the constraint you requested in your original question. More specifically due to this part

where TP : AbstractBasePresenter<T>

It seems you want to constrain TP to be an interface as well. You may try the below piece of code:

public interface IView<TV, TE, TP, T> : IView
    where TV : IViewModel
    where TE : IEditModel
    where TP : IPresenter<T>
{
}

Constraints on T are no more needed, because IPresenter<T> has none. Of course, you could adapt armen.shimoon's answer in a similar manner. The point is to replace AbstractBasePresenter<T> constraint with IPresenter<T> constraint.

like image 44
pbalaga Avatar answered Sep 17 '22 15:09

pbalaga