[英]Can derived C# interface definitions 'override' function definitions?
I am defining a geenral API which will have a number of more specific derivations and want to know if C# interfaces are powerful enough to model this, and if so how, and if not how else I might model this. 我正在定义一个通用API,它将具有许多更特定的派生功能,并且想知道C#接口是否足够强大以进行建模,如果可以的话,以及如何进行建模。
To illustrate what I am trying to do imagine an authentication API with a general interface with an Authenticate function which takes an abstract AuthenticationToken. 为了说明我正在尝试做的事情,请想象一个具有通用接口的身份验证API,该接口带有一个Authenticate函数,该函数需要一个抽象的AuthenticationToken。 I want to then create more specific forms of this interface with as shown ..
然后,我想用如下所示创建此接口的更特定形式。
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)
{
}
}
My intention is that the derived interface is constrained to comply with the high level form but that is not how C# interprets this. 我的意图是,派生的接口必须遵守高级格式,但这不是C#对此的解释。
Best Regards 最好的祝福
Help me John Skeets .. you're my only hope. 帮我约翰·斯基特(John Skeets)..你是我唯一的希望。 (Sorry .. my Star Wars BluRays have arrived!)
(对不起..我的《星球大战蓝光》已经到来!)
This is what you want: 这就是你想要的:
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)
{
}
}
Generics with constraints! 有约束的泛型!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.