Can I force subclasses to override a method without making it abstract?

前端 未结 7 693
别那么骄傲
别那么骄傲 2021-01-02 12:22

I have a class with some abstract methods, but I want to be able to edit a subclass of that class in the designer. However, the designer can\'t edit the subclass unless it

7条回答
  •  情深已故
    2021-01-02 12:34

    Well you could do some really messy code involving #if - i.e. in DEBUG it is virtual (for the designer), but in RELEASE it is abstract. A real pain to maintain, though.

    But other than that: basically, no. If you want designer support it can't be abstract, so you are left with "virtual" (presumably with the base method throwing a NotImplementedException).

    Of course, your unit tests will check that the methods have been implemented, yes? ;-p

    Actually, it would probably be quite easy to test via generics - i.e. have a generic test method of the form:

    [Test]
    public void TestFoo() {
      ActualTest();
    }
    [Test]
    public void TestBar() {
      ActualTest();
    }
    
    static void ActualTest() where T : SomeBaseClass, new() {
      T obj = new T();
      Assert.blah something involving obj
    }
    

提交回复
热议问题