views:

62

answers:

2

I am trying to figure out a way to force all of my Interfaces to include properties of the same name/type.

For example: I have two Interfaces; IGetAlarms and IGetDiagnostics. Each of the Interfaces will contain properties that are specific to the Interface itself, however I want to force the two Interfaces (and all other Interfaces that may be added later) to include properties of the same name. So, the result may look something like the this:

interface IGetAlarms
{
    string GetAlarms();
    DateTime LastRuntime { get; set; }
}

interface IGetDiagnostics
{
    string GetDiagnostics();
    DateTime LastRuntime { get; set; } 
}

Notice that both Interfaces include a DateTime property named LastRuntime.

I would like to know if there is some way I can force other Interfaces that will be added later to include the DateTime LastRuntime property. I have naively attempted to have all my Interfaces implement another Interface (IService) - which includes the LastRuntime property. However, that doesn't solve my problem as that simply forces the class to implement the property - not all the Interfaces.

Thanks.

+3  A: 

An interface can inherit from other interfaces.

interface IDerived : IBase
{
    string Foo { get; set; }
}

interface IBase
{
    DateTime LastRunDate { get; set; }
}

Any class deriving from IDerived will have to implement the methods/properties of IBase as well.

class Derived : IDerived 
{
    #region IDerived Members

    public string Foo { get; set; }

    #endregion

    #region IBase Members

    public DateTime LastRunDate {get;set;}

    #endregion
}
Anthony Pegram
+1  A: 

If I understand your question correctly, you want to force a class to implement a number of different interfaces, the list of interfaces will grow with time but will have some properties in common.

The common property part you have solved with your IService interface. Something like this, I presume

interface IService
{
    DateTime LastRuntime { get; set; }
}

interface IGetAlarms : IService
{
    string GetAlarms();

}

interface IGetDiagnostics : IService
{
    string GetDiagnostics();
}

The growing list of interfaces that a class will have to implement you can also solve in a similar fashion. Create a "composite" interface which inherits from all the interfaces you wish your class to implement

interface IComposite : IGetAlarms, IGetDiagnostics {}

class MyClass : IComposite
{
    ...
}

When you let the IComposite interface inherit a new interface, the class will have implement the new interface too.

EDIT

In response to your clarification; in that case you should not share the specification of the LastRuntime property, but declare it in each individual interface. In the implementing class you can use Explicit interface member implementation

class MyClass : IComposite
{
    DateTime IGetAlarms.LastRuntime { get; set; }
    DateTime IGetDiagnostics.LastRuntime { get; set; }
    ...
}

However, AFAIK it is not possible to force the implementing class to explicitly implement each individual interface

Peter
I should have mentioned in my initial post that the properties I would like to enforce in all the Interfaces must be specific to each Interface. So, using my initial example, the class that implements IGetAlarms and IGetDiagnostics will be able to store different values for the IGetAlarms.LastRuntime and IGetDiagnostics.LastRuntime properties. The problem I'm trying to solve here is making sure all Interfaces support their own implementation of LastRuntime. I'm unable to use abstract classes in this case, because my derived class needs to be able to implement multiple services.
Jed
Thanks for your input, Peter.
Jed