2010-10-20 55 views
0

我有類似這樣的課程設置:在用戶界面中使用許多類從基繼承

<DataContract()> _ 
Public MustInherit Class SystemTaskProcessBase 

    Public MustOverride ReadOnly Property Name() As String 
    Public MustOverride ReadOnly Property Description() As String 

    Public MustOverride Property Result() As SystemTaskResult 

    <DataMember()> _ 
    Private _TaskID As Integer = 0 
    Public Property TaskID() As Integer 
     Get 
      Return _TaskID 
     End Get 
     Set(ByVal value As Integer) 
      _TaskID = value 
     End Set 
    End Property 

End Class 

<DataContract()> _ 
Public Class RebootSystemTaskProcess 
    Inherits SystemTaskProcessBase 

    Private _Name As String = "Reboot System" 
    Public Overrides ReadOnly Property Name() As String 
     Get 
      Return _Name 
     End Get 
    End Property 

    Private _Description As String = "Task for the client to reboot itself internally." 
    Public Overrides ReadOnly Property Description() As String 
     Get 
      Return _Description 
     End Get 
    End Property 

    <DataMember()> _ 
    Public _Result As SystemTaskResult = SystemTaskResult.NotProcessed 
    Public Overrides Property Result() As SystemTaskResult 
     Get 
      Return _Result 
     End Get 
     Set(ByVal value As SystemTaskResult) 
      _Result = value 
     End Set 
    End Property 

End Class 

<DataContract()> _ 
Public Class DeleteFileSystemTaskProcess 
    Inherits SystemTaskProcessBase 

    Private _Name As String = "Delete File" 
    Public Overrides ReadOnly Property Name() As String 
     Get 
      Return _Name 
     End Get 
    End Property 

    Private _Description As String = "Task for the client to delete a local file." 
    Public Overrides ReadOnly Property Description() As String 
     Get 
      Return _Description 
     End Get 
    End Property 

    <DataMember()> _ 
    Public _Result As SystemTaskResult = SystemTaskResult.NotProcessed 
    Public Overrides Property Result() As SystemTaskResult 
     Get 
      Return _Result 
     End Get 
     Set(ByVal value As SystemTaskResult) 
      _Result = value 
     End Set 
    End Property 

    <DataMember()> _ 
    Private _File As FileInfo 
    Public Property File() As FileInfo 
     Get 
      Return _File 
     End Get 
     Set(ByVal value As FileInfo) 
      _File = value 
     End Set 
    End Property 

End Class 

我需要在客戶端系統上使用這些類,也需要能夠創造這些「任務「通過管理界面。每個繼承基類的類(Task)都可以擁有自己的屬性,這些屬性對每個類都是唯一的,但同時共享相同的基類屬性。例如,上面顯示的是重新啓動任務和刪除文件任務,刪除文件任務需要知道要刪除哪個文件,因此具有該屬性。但重新啓動任務不需要此屬性。所以當管理應用程序創建這些任務時,它不應該爲重新啓動任務提供文件屬性的文本框。可能會有更多的任務在以後使用完全不同的屬性創建。

我該如何去爲WinForms管理應用程序提供一種方法來枚舉每個類到ListView中,並允許用戶創建這些任務並填充每個類將具有的動態屬性?

所需的功能是創建一個任務表單,根據需要爲屬性創建可用的動態控件,具體取決於每個類中的公共屬性,但同時也具有基類屬性。

任何想法表示讚賞。

感謝, 斯科特

回答

0

如果你需要一個高度可定製的和可擴展的解決方案,然後着手做這將是迄今爲止驅動方法的一種方式。在我以前的工作之一中,我們必須根據客戶端偏好創建一個動態UI,其中字段1適用於一個客戶端,但其他客戶端不需要。擴展這種方法你的問題,你需要有數據庫表

TasksTable with following columns 
1.TaskId 
2.TaskName 
3.ClassName (you will use reflection to create an instance of it) 

另一個表與任務的實際領域。現在讓我們稱它爲TaskFields吧。

TaskFields Table 
1. TaskFieldId 
2. Property (You can create another field to store the Name if you need to) 
3. Enabled (bit) 
4. SpecialFormat <if you need to apply this and customize it based on some preference) 

這些都是相當小的表格。在應用程序啓動時加載它們。在您的應用程序中,將您的ListBox綁定到TasksTable中的TaskName字段。 選擇特定項目時,根據所選項目的TaskFields表中的值動態創建控件。根據ClassName字段創建類,並將創建綁定關聯到生成的控件。

這很複雜,但您獲得的優勢是您可以切換Off/On控制以顯示。

編輯: 如果您打算使用屬性方法,以下可能會得心應手。根據您的示例,您已經將您需要的屬性標記爲Datamember。因此,您不必定義任何自定義屬性,除非您有其他屬性不會用於用戶輸入,而是將其自身標記爲用於其他目的的DataMember。

Dim type As Type = GetType(yourClass) 
Dim properties As PropertyInfo() = type.GetProperties() 
Dim neededProperties = (From [property] In propertiesLet attributes = DirectCast([property].GetCustomAttributes(GetType(DataMemberAttribute), False), Attribute()) Where attributes.Count() > 0[property]).ToList() 
+0

感謝您的回覆。我現在真的想把這個保留在數據庫之外。發佈我的問題後,我開始閱讀類和字段的自定義屬性,然後能夠讀取這些標誌值以確定該字段是否應該爲其創建控件。我認爲它基本上與你建議的一樣,只是消除了數據庫需求。 – ScottN 2010-10-21 15:47:35

+0

@ScottN。請參閱編輯使用反射來獲取所需的屬性。 – SKG 2010-10-21 16:34:08