【问题标题】:generic class inherits generic interface but generates and error at compile泛型类继承泛型接口但在编译时生成并出错
【发布时间】:2018-01-15 05:14:51
【问题描述】:

我有如下界面:

public interface IModel<T>
{
    List<T> responseIds { get; set; }
}

我有一个实现接口的通用类:

class SFDCResponse<T> : SFDCBaseResponse, IModel<T>
{
    public List<T> responseIds;
}

最后,我这样定义一个类:

class SFDCAccounts : SFDCResponse<MyClass> { }

但是,在编译时出现以下错误:

SFDCResponse<T> does not implement interface member IModel<T>.responseIds

难道不能让泛型类​​继承泛型接口并让编译器在编译时验证该类吗?

【问题讨论】:

  • 您知道有代码操作会自动生成您的界面,因此您不会遇到这些问题
  • 如果您坚持为 C# 推荐的大小写约定,您的问题会更加清晰。属性为 PascalCase,字段为 camelCase MSDN Reference。您可以看到该接口声明了您的类所缺少的属性。

标签: c# generics inheritance interface


【解决方案1】:

responseIds 是接口中的Property 和实现类中的Field。将其设为属性,这应该可以解决您的问题。

【讨论】:

  • 谢谢。就是这样,当我粘贴它时我意识到了。
【解决方案2】:

好吧,看看我自己的帖子,我意识到我的 SFDCResponse 类将 responseIds 实现为一个字段,而不是一个属性。我刚刚更正为:

class SFDCResponse<T> : SFDCBaseResponse, IModel<T>
{
    public List<T> responseIds { get; set; }
}

应用程序已编译。有时,只要经历这个过程,你就会得到你自己的答案。

【讨论】:

  • 由橡皮鸭固定。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-05
  • 2019-06-25
  • 2020-03-02
  • 1970-01-01
  • 1970-01-01
  • 2020-01-23
相关资源
最近更新 更多