Search code examples
c#inheritanceinterfacederived-class

How to define enums in a class abiding by an interface


The interface is:

public interface CommonPluginInterface
{
    string GetPluginName();
    string GetPluginType();
    bool ElaborateReport();
}

now I want all derived classes to identify themselves through a string and an enum. For the string it's easy for it's hard coded:

public class PluginReport_Excel : MarshalByRefObject, CommonPluginInterface
{
    public string GetPluginName()
    {
        return "Foo";
    }
}

but additionally I want it to identify through an enum also. So I thought about putting in the interface but interface can't contain members.

So I thought about making

public class CommonPluginClass
{
    private enum ePluginType { UNKNOWN, EXCEL, EXCEL_SM, RTF}
    private ePluginType pluginType;
}

and making the derived class derive ALSO from that but this is not possible for it says:

Class 'PluginReport_Excel' cannot have multiple base classes: 'MarshalByRefObject' and 'CommonPluginClass'

and I need MarshalByRefObject. Thanks for any help.


Solution

  • Define an enum separately and define it as a return type for the GetPluginType method.

    public enum ePluginType 
    { 
        UNKNOWN, 
        EXCEL, 
        EXCEL_SM, 
        RTF
    } 
    
    public interface CommonPluginInterface
    {
        string GetPluginName();
        ePluginType GetPluginType();
        bool ElaborateReport();
    }
    
    public class PluginReport_Excel : MarshalByRefObject, CommonPluginInterface
    {
        public ePluginType GetPluginType()
        {
            return ePluginType.EXCEL;
        }
    
        //implement other interface members
    }