Interfaces cannot declare types

前端 未结 4 851
终归单人心
终归单人心 2020-12-15 20:24

I have an abstract class in an API that is used by methods in another assembly. The class has a nested enum defined inside it, a bit like this:

abstract publ         


        
相关标签:
4条回答
  • 2020-12-15 20:45

    Oh yes, the solution is to use an abstract class if you need such implementation. Abstract classes are not a bad design and are certainly useful in situations like this.

    If you insist on using interfaces, I'm afraid you'd have to go with the solution from p.s.w.g and break a rule or two (those are just guidelines anyway, really).

    0 讨论(0)
  • 2020-12-15 20:48

    You must declare a type as public in namespace or outside of namespace and then declare a method of this type in the interface

    0 讨论(0)
  • 2020-12-15 20:55

    abstract class and interface are different things. abstarct class is abstraction, higher than your domain model and interface is the contract (behavior) of your domain entity. You can use both in your solution as necessary. In concrete scenario status is not behavior it just state of entity. I think abstract class is more reliable choice.

    0 讨论(0)
  • 2020-12-15 21:04

    As the error indicates, you just have to pull the definition of Status outside of the interface. I understand that it breaks encapsulation, but there's really no way around this. I suggest you change the name of Status to something which indicates a strong relation to Thing -- ThingStatus should do the trick.

    enum ThingStatus { Accepted, Denied, Pending };
    
    public interface Thing
    {
        ThingStatus status { get; }
        etc...
    }
    
    0 讨论(0)
提交回复
热议问题