派生的C#接口定义可以"覆盖"函数定义吗?

How*_*May 1 c# interface

我正在定义一个geenral API,它将有许多更具体的派生,并想知道C#接口是否足够强大,可以对此进行建模,如果是这样,如果不是,我将如何建模.

为了说明我想要做的事情,想象一个身份验证API,它带有一个带有Authenticate函数的通用接口,它接受一个抽象的AuthenticationToken.我想再创建这个界面的更具体的形式,如图所示..

abstract class AuthenticationToken
{
}

interface IAthentication
{
    bool Authenticate(string name, AuthenticationToken token);
}

class DoorKey : AuthenticationToken
{
}

interface IDoorAthentication : IAthentication
{
    bool Authenticate(string name, DoorKey token);
}

class DoorUnlocker : IDoorAthentication
{

    public bool Authenticate(string name, DoorKey token)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

我的意图是派生接口被约束为符合高级形式,但这不是C#如何解释它.

最好的祝福

帮帮我John Skeets ..你是我唯一的希望.(对不起..我的星球大战蓝光已经到了!)

Eni*_*ity 5

这就是你想要的:

abstract class AuthenticationToken
{
}

interface IAthentication<T> where T : AuthenticationToken
{
    bool Authenticate(string name, T token);
}

class DoorKey : AuthenticationToken
{
}

interface IDoorAthentication : IAthentication<DoorKey>
{
}

class DoorUnlocker : IDoorAthentication
{
    public bool Authenticate(string name, DoorKey token)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

具有约束的泛型!