c# - 能否导出C#接口(interface)定义 'override'函数定义?

标签 c# interface

我正在定义一个通用 API,它将有许多更具体的派生,并且想知道 C# 接口(interface)是否足够强大来对此建模,如果是,如何建模,如果不是,我还可以如何建模。

为了说明我正在尝试做的事情,请想象一个具有通用接口(interface)的身份验证 API,该接口(interface)具有一个采用抽象 AuthenticationToken 的 Authenticate 函数。然后我想创建此界面的更具体形式,如图所示..

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)
    {
    }
}

我的意图是派生接口(interface)被限制为符合高级形式,但这不是 C# 解释它的方式。

最好的问候

帮助我 John Skeets .. 你是我唯一的希望。 (抱歉......我的星球大战蓝光已经到了!)

最佳答案

这就是你想要的:

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)
    {
    }
}

有约束的泛型!

关于c# - 能否导出C#接口(interface)定义 'override'函数定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7792244/

相关文章:

c# - ASP.NET 4.5.2 MVC 项目模板中的身份验证系统是否适合在实际应用程序中使用?

c# - 如果我扩展一个 c# 接口(interface),我可以吃一个电话吗?

golang,如何满足同一个包中多个文件的接口(interface)?

java - 戈朗 : what's the point of interfaces when you have multiple inheritence

c# - 绑定(bind) WPF 组合框并将其值显示到 TextBox

c# - 生产 ASP.net 应用程序比开发慢

c# - 共享 1 个网站和 1 个 Web API 的 Azure 应用服务

go - 获取基于原始类型的类型的reflect.Kind

typescript - 计算 key ,作为接口(interface) key ,来自枚举成员

c# - 使用缓冲流与在源流上使用读取方法读取相同数量的字节有何不同?