代码之家  ›  专栏  ›  技术社区  ›  Jim Fell

如何在C#中实现特定于派生类的枚举接口?

  •  0
  • Jim Fell  · 技术社区  · 7 年前

    interface MyInterface
    {
        MyObject Set(REGISTER register, PIN pin);
    }
    

    REGISTER PIN 是枚举。我遇到的问题是 注册 在我的 MyBaseClass 别针 MyDerivedClass MyBaseClass.REGISTER 在界面中,但这对 别针 . 每一个推导 我的基类 别针 . 我发现 this other answer on SO ,这似乎部分解决了问题,但不清楚接下来应该如何在中实现该函数 Myderived类 . 有人有解决办法吗?

    1 回复  |  直到 7 年前
        1
  •  4
  •   Andrew Williamson    7 年前

    尝试使用泛型:

    interface MyInterface<TPin>
    {
        MyObject Set(Register register, TPin pin);
    }
    

    class MyImplementation : MyInterface<MyPin>
    {
        MyObject Set(Register register, MyPin pin)
        {
            // ...
        }
    }
    

    这意味着你不能有 MyInterface MyInterface<SomePin> 物体。这很有意义-如果每个实现的枚举不同,那么给定 我的接口