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

前端 未结 7 630
别那么骄傲
别那么骄傲 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:23

    I know its not quite what you are after but you could make all of your stubs in the base class throw the NotImplementedException. Then if any of your subclasses have not overridden them you would get a runtime exception when the method in the base class gets called.

    0 讨论(0)
  • 2021-01-02 12:28

    To use ms as an example...

    Microsoft does this with the user control templates in silverlight. #if is perfectly acceptable and it is doubtful the the tooling will work around it anytime soon. IMHO

    0 讨论(0)
  • 2021-01-02 12:29

    Note that "DesignMode" is not set in the constructor. It's set after VS parses the InitializeComponents() method.

    0 讨论(0)
  • 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<Foo>();
    }
    [Test]
    public void TestBar() {
      ActualTest<Bar>();
    }
    
    static void ActualTest<T>() where T : SomeBaseClass, new() {
      T obj = new T();
      Assert.blah something involving obj
    }
    
    0 讨论(0)
  • 2021-01-02 12:39

    You could use the reference to implementation idiom in your class.

    public class DesignerHappy
    {
        private ADesignerHappyImp imp_;
    
        public int MyMethod()
        {
            return imp_.MyMethod()    
        }
    
        public int MyProperty
        {
            get { return imp_.MyProperty; }
            set { imp_.MyProperty = value; }
        }
    }
    
    public abstract class ADesignerHappyImp
    {
        public abstract int MyMethod();
        public int MyProperty {get; set;}
    }
    

    DesignerHappy just exposes the interface you want but forwards all the calls to the implementation object. You extend the behavior by sub-classing ADesignerHappyImp, which forces you to implement all the abstract members.

    You can provide a default implementation of ADesignerHappyImp, which is used to initialize DesignerHappy by default and expose a property that allows you to change the implementation.

    0 讨论(0)
  • 2021-01-02 12:46

    The Component class contains a boolean property called "DesignMode" which is very handy when you want your code to behave differently in the designer than at runtime. May be of some use in this case.

    0 讨论(0)
提交回复
热议问题