2010-06-08 44 views
4

好吧,我正在推翻這一點。有沒有一種方法可以組合接口和屬性,以便實現類中的屬性屬性滿足接口合同?使用屬性來滿足界面要求

在我的應用程序中,我想展示一系列活動,這些活動是系統中的事件聚合,例如新消息,正在創建/分配/更新的新任務等。它只是一個快速的手發生了什麼事情的清單。

我想過使用IIsActivity接口來標記需要成爲該聚合的一部分的實體。我只需要顯示活動標題,發生日期以及鏈接到應用程序中的相關區域。我不想在繼承類中執行額外的屬性,但它們只是最終重複信息。例如,任務具有AssignedOn(DateTime),並且消息具有CreatedOn(DateTime)。這兩個都將滿足要求被視爲活動的日期要求。我不想要的是僅針對活動日期的單獨屬性。同樣的事情可以去標題(消息有一個標題屬性,任務有一個名稱屬性)。因此,我基本上希望能夠說:「給我任何東西,這是一個IIsActivity。在每個實現者中,我希望有一個標記了ActivityTitle屬性的屬性,以及一個具有ActivityDate屬性的屬性。 「

太多了?

回答

1

我認爲你最好的方法是使用明確的接口實現。

public interface IActivity 
{ 
    DateTime OccurredOn { get; } 
} 

public class Task : IActivity 
{ 
    public DateTime AssignedOn 
    { 
    get { /* implemenation */ } 
    } 

    DateTime IActivity.OccurredOn 
    { 
    get { return AssignedOn; } 
    } 
} 

public class Message : IActivity 
{ 
    public DateTime CreatedOn 
    { 
    get { /* implemenation */ } 
    } 

    DateTime IActivity.OccurredOn 
    { 
    get { return CreatedOn; } 
    } 
} 

然後你可以使用你的類像這樣:

public static void Main() 
{ 
    Task task = new Task(); 
    Console.WriteLine(task.AssignedOn); // OK 
    Console.WriteLine(task.OccurredOn); // Compile Error 
    IActivity activity = task as IActivity; 
    if (activity != null) 
    { 
    Console.WriteLine(activity.AssignedOn); // Compile Error 
    Console.WriteLine(activity.OccurredOn); // OK 
    } 
} 
0

這不正是你的設計,但我已經使用屬性來執行一些我的課的性質有一個理想值。我耗費了web服務,我要發一些信息,一些字段的要求進行價值,別人不...

[Serializable] 
    [AttributeUsage(AttributeTargets.Property)] 
    public class RequiredAttribute : Attribute 
    { 
     private CheckType[] _requiredtype; 
     public RequiredAttribute(params CheckType[] type) 
     { 
      _requiredtype = type; 
     } 
     public CheckType[] Requires 
     { 
      get { return _requiredtype; } 
     } 
     public static bool CheckRequirements(object applyto, out string errormessages) 
     { ... } 

     private static bool RequiredSucceeded(object applyto, StringBuilder resultmessage) 
     { ... } 
    } 
    [Serializable] 
    public enum CheckType 
    { 
     HasValue,  // Checks that the property value is not null 
     CheckMinRequirements, // Will enforce the validation of properties on defined types 
     IsNotNullorEmpty, // For strings 
     HasElements, // Elements exist on arrays 
     ElementsRequirements // for collections 
    } 

現在有使用屬性

[Serializable] 
    public class PurchaseInsurance 
    { 
     [Required(CheckType.IsNotNullorEmpty)] 
     public string PartnerBookingID 
     { 
      get; 
      set; 
     } 
     [Required(CheckType.IsNotNullorEmpty)] 
     public string Currency 
     { 
      get; 
      set; 
     } 
     public string ReferringURL; 

     [Required(CheckType.HasValue, CheckType.CheckMinRequirements)] 
     public PurchaserInfo Purchaser 
     { 
      get; 
      set; 
     } 
     [Required(CheckType.HasValue, CheckType.ElementsRequirements)] 
     public InsuranceProduct[] Products 
     { 
      get; 
      set; 
     } 
... 
} 
一個類的實例

在將數據發送到webService之前,我將檢查每個屬性是否符合它們的屬性標記。

0

雖然CLR支持它,但C#不支持接口實現別名。你可以在VB.NET實現它,但:

Public Interface IActivity ' this can even be in C# ' 
    ReadOnly Property OccurredOn() As DateTime 
End Interface 

Public Class Task 
    Implements IActivity 

    Public ReadOnly Property AssignedOn() As DateTime Implements IActivity.OccurredOn 
    Get 
     ' implementation... ' 
    End Get 
    End Property 

End Class 

Public Class Message 
    Implements IActivity 

    Public ReadOnly Property CreatedOn() As DateTime Implements IActivity.OccurredOn 
    Get 
     ' implementation... ' 
    End Get 
    End Property 

End Class 

所以,由Brian Gideon提到,顯式接口實現是什麼來在C#更接近你的目標。

相關問題