Universal interface implementation

I have a common interface:

public interface IUnauthorizedRequestRespondable<out T> where T:class 
{
    T GetResponseForUnauthorizedRequest();
}  

(I'm not sure why Resharper recommended T "out", but that is not a question).
In my script, the object returned GetResponseForUnauthorizedRequestis always of type implementing the interface.

So, all interface implementations look like this:

public class SignInReturnedObject : IUnauthorizedRequestRespondable<SignInReturnedObject>  

(class name and type in parentheses always match).
Which seems a bit inconvenient - is there a tidier way to tell the compiler that the interface method returns the type of which it is part?

Thank.

+3
source share
5 answers

If the only way you want to use this template would be this way:

public interface IUnauthorizedRequestRespondable<T> where T:IUnauthorizedRequestRespondable<T>
{
    T GetResponseForUnauthorizedRequest();
}

, - .

, . , , .

+2

, # .

. , , self-, #. , , this, , self, , , - :

public interface IUnauthorizedRequestRespondable { 
    self GetResponseForUnauthorizedRequest(); 
}   

... , self, SignInReturnedObject, SignInReturnedObject, , , #: -)

+4

, . .

, .NET IEquatable inteface - , .

, , .

0

, , ,

public interface ICastUnauthorizedRequestRespondable : IUnauthorizedRequestRespondable<SignInReturnedObject>
{
}  
0

T ( , ), .

public class SignInReturnedObject : IUnauthorizedRequestRespondable<ANYCLASS>
0

Source: https://habr.com/ru/post/1768984/


All Articles