【问题标题】:How to force multiple Interfaces to include the same properties?如何强制多个接口包含相同的属性?
【发布时间】:2010-03-25 03:33:42
【问题描述】:

我正在尝试找出一种方法来强制我的所有接口包含相同名称/类型的属性。

例如:我有两个接口; IGetAlarms 和 IGetDiagnostics。每个接口都将包含特定于接口本身的属性,但是我想强制这两个接口(以及以后可能添加的所有其他接口)包含同名的属性。因此,结果可能如下所示:

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

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

请注意,两个接口都包含一个名为 LastRuntime 的 DateTime 属性。

我想知道是否有某种方法可以强制稍后添加的其他接口包含 DateTime LastRuntime 属性。我天真地试图让我的所有接口实现另一个接口 (IService) - 其中包括 LastRuntime 属性。但是,这并不能解决我的问题,因为这只是强制类实现属性 - 而不是所有接口。

谢谢。

【问题讨论】:

    标签: c# inheritance properties interface


    【解决方案1】:

    一个接口可以从其他接口继承。

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

    任何从 IDerived 派生的类也必须实现 IBase 的方法/属性。

    class Derived : IDerived 
    {
        #region IDerived Members
    
        public string Foo { get; set; }
    
        #endregion
    
        #region IBase Members
    
        public DateTime LastRunDate {get;set;}
    
        #endregion
    }
    

    【讨论】:

      【解决方案2】:

      如果我正确理解你的问题,你想强制一个类实现多个不同的接口,接口列表会随着时间的推移而增加,但会有一些共同的属性。

      您使用IService 接口解决的公共属性部分。我猜是这样的

      interface IService
      {
          DateTime LastRuntime { get; set; }
      }
      
      interface IGetAlarms : IService
      {
          string GetAlarms();
      
      }
      
      interface IGetDiagnostics : IService
      {
          string GetDiagnostics();
      }
      

      类必须实现的接口列表越来越多,您也可以用类似的方式解决。创建一个“复合”接口,该接口继承自您希望类实现的所有接口

      interface IComposite : IGetAlarms, IGetDiagnostics {}
      
      class MyClass : IComposite
      {
          ...
      }
      

      当你让IComposite接口继承一个新接口时,该类也将实现新接口。

      编辑

      响应您的澄清;在这种情况下,您不应该共享LastRuntime 属性的规范,而是在每个单独的接口中声明它。在实现类中可以使用Explicit interface member implementation

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

      然而,AFAIK 不可能强制实现类显式实现每个单独的接口

      【讨论】:

      • 我应该在我最初的帖子中提到,我想在所有接口中强制执行的属性必须特定于每个接口。因此,使用我的初始示例,实现 IGetAlarms 和 IGetDiagnostics 的类将能够为 IGetAlarms.LastRuntime 和 IGetDiagnostics.LastRuntime 属性存储不同的值。我在这里要解决的问题是确保所有接口都支持它们自己的 LastRuntime 实现。在这种情况下我无法使用抽象类,因为我的派生类需要能够实现多个服务。
      【解决方案3】:

      这完全取决于您需要该界面的用途。您可以使用泛型来强制执行指定模式,但如果它们都具有相同的签名,则不能单独强制执行每个模式。

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

      这将强制以下类分别实现每个:

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

      它是“T ignore”参数,它强制每个参数单独实现,并且由于它具有默认值,您可以忽略该参数,除非使用反射调用它。

      但显然这不适用于属性,因此必须使用 getter/setter 方法来实现它们。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-21
        • 1970-01-01
        相关资源
        最近更新 更多