您好我已经制作了一个看起来像这样的插件界面
public interface IPluginInterface :IEquatable<IPluginInterface>
{
string Maker { get; }
string Version { get; }
void Do();
void Do_two();
}
我试过了,但还没有找到任何方法让字符串Maker和Version可选, 我想我必须设置一个布尔值Equals,但不知道如何。 谢谢你的帮助
答案 0 :(得分:5)
如果您在界面中声明了它们,则必须才能实现。
您不能在接口上声明可选成员。
有几种选择:
答案 1 :(得分:1)
您不能将任何接口方法标记为可选 - 要么实现整个接口,要么根本不实现它!
您可以考虑将此界面拆分为两个不同的界面。
答案 2 :(得分:1)
将界面分成几个:
public interface IPluginInterface : IEquatable<IPluginInterface>
{
string Maker { get; }
string Version { get; }
}
public interface IPluginWithOptionA : IPluginInterface
{
void Do();
}
public interface IPluginWithOptionB : IPluginInterface
{
void Do_two();
}
您可以实现一个或多个接口
public class MyPlugin : IPluginWithOptionA, IPluginWithOptionB
{
public bool Equals(IPluginInterface other)
{
throw new NotImplementedException();
}
public string Maker
{
get { throw new NotImplementedException(); }
}
public string Version
{
get { throw new NotImplementedException(); }
}
public void Do_two()
{
throw new NotImplementedException();
}
public void Do()
{
throw new NotImplementedException();
}
}
答案 3 :(得分:0)
如果您希望此方法可选,则接口是错误的。但是你可以将它们放入一个抽象的基类中。