How to force overriding a method in a descendant, without having an abstract base class?

前端 未结 14 973
孤城傲影
孤城傲影 2020-12-09 07:24

Question Heading seems to be little confusing, But I will Try to clear my question here.

using System;
using System.Collections.Generic;
usi         


        
14条回答
  •  佛祖请我去吃肉
    2020-12-09 08:06

    This is pretty old, but we have a somewhat similar situation. The base class loads a configuration file and sets up base class defaults. However, the configuration file could also contain default values for inherited classes.

    This is how we got the combined functionality.

    Base class methods

    private void processConfigurationFile()
    {
        // Load and process the configuration file
        // which happens to be an xml file.
        var xmlDoc = new XmlDocument();
        xmlDoc.Load(configurationPath);
    
        // This method is abstract which will force
        // the inherited class to override it.
        processConfigurationFile(xmlDoc);
    }
    
    protected abstract void processConfigurationFile(XmlDocument document);
    

提交回复
热议问题