【发布时间】:2022-01-14 22:17:57
【问题描述】:
我在 c# 中有一个接口,如下所示:
public interface IMyInterfaceA
{
string Name { get; }
int Id { get; set; }
}
我想扩展此接口以包含其他属性。
例如:
public interface IMyInterfaceB: IMyInterfaceA
{
string newProp { get; }
}
问题是这样的语法是无效的。它要求我实现 IMyInterfaceA, 所以我的界面看起来像:
public interface IMyInterfaceB: IMyInterfaceA
{
string newProp { get; }
public string Vendor => throw new NotImplementedException();
public int VendorId { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
}
我使用继承的所有目标是让我不需要再次拥有这些属性。
IMyInterfaceA是我无法更改的核心接口。
如何在不重写基本属性的情况下实现扩展?
编辑: 我的代码没有错误。我错误地创建了一个类而不是一个接口。
【问题讨论】:
-
“问题是这样的语法无效。”嗯,很确定是的;否则到底是怎么说的,它说了什么?
-
你收到了什么错误信息?
-
如果我不实现
IMyInterfaceA(例如添加属性),它会在IMyInterfaceB: IMyInterfaceA这个词后面显示红线并说“实现接口” -
问题中的代码没问题。你确定你没有创建
class IMyInterfaceB : IMyInterfaceA吗? (注class) -
天哪!你说得对!我会删除帖子。 Tnx。