【问题标题】:getting "interface list is not an interface" in a simple base class and interface in generic implementation in c#在 C# 中的通用实现中获取简单基类和接口中的“接口列表不是接口”
【发布时间】:2020-06-26 21:01:58
【问题描述】:

我得到类型

接口列表中的“BaseServiceTypeRecord”不是 界面

不知道为什么。

基类和接口

public interface IBaseServiceTypeRecord<TType> where TType : struct, IConvertible
{
    TType Type { get; }

    string Name { get; }
}

public abstract class BaseServiceTypeRecord<TType> : IBaseServiceTypeRecord<TType> where TType : struct, IConvertible
{
    private readonly TType _type;
    private readonly string _name;

    public BaseServiceTypeRecord(TType type, string name)
    {
        _type = type;
        _name = name;
    }

    public TType Type => _type;
    public string Name => _name;
}

实施

public struct AssetsServiceTypesRecord : BaseServiceTypeRecord<ServicesTypes>, IBaseServiceTypeRecord<ServicesTypes>
{
    public AssetsServiceTypesRecord(ServicesTypes type, string name)
    {
    }
}

ServicesTypes 枚举

public enum ServicesTypes
{
    Default,
    Database,
    Secure,
    Repo,
    HTTPS,
    Slugify,
    Sites,
    Blazor,
    Configuration,
    Cookie,
    QueryExpression,
    Helper
}

我只想传入一个枚举类型

【问题讨论】:

  • 我不相信枚举可以用作类型约束,你想在这里完成什么?
  • @JSteward 能够定义我传递给对象的枚举
  • 但是你会传递整个枚举类型,这是没有意义的,这似乎是一个 xy 问题,你想在这里真正解决什么?
  • @JSteward 基类可以包含任何类型的枚举,并且我定义了它在基类/接口中的哪一个,以便实现的类知道哪个枚举在起作用
  • 为什么要(重新)在结构上添加接口声明?

标签: c# class generics interface


【解决方案1】:

将类型从结构更改为类,并按预期工作。

代码

public abstract class AssetsServiceTypesRecord : BaseServiceTypeRecord<ServicesTypes>
{
    public AssetsServiceTypesRecord(ServicesTypes type, string name) : base(type, name)
    {
        
    }
}

【讨论】:

  • 那是因为 C# 中的结构不支持继承。
  • @LasseV.Karlsen yepp 想出了一个
猜你喜欢
  • 2013-03-23
  • 2014-08-31
  • 2017-02-02
  • 1970-01-01
  • 2016-10-07
  • 1970-01-01
  • 1970-01-01
  • 2016-07-22
  • 1970-01-01
相关资源
最近更新 更多