开发者

How to force multiple Interfaces to include the same properties?

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.


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
}


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


It really depends on exactly what you need the interface for. You can use generics to enforce the implementation of a specified pattern, but you can't enforce the implementation of each individually if they all have identical signatures.

public interface IA<T> where T: class
{
    void DoIt(T ignore = null);
}
public interface IB : IA<IB>
{
}
public interface IC : IA<IC>
{
}

That would force the following class to implement each separately:

public class D : IB, IC
{
    public void DoIt(IB ignore = null) { }
    public void DoIt(IC ignore = null) { }
}

It's the "T ignore" parameter that forces each one to be implemented separately, and since it has a default value, you can just ignore that parameter unless calling it using reflection.

But obviously this doesn't work with properties, so they would have to be implemented using getter/setter methods.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜