【问题标题】:Using many classes in GUI that inherit from a base在 GUI 中使用许多从基类继承的类
【发布时间】:2010-10-20 15:39:36
【问题描述】:

我有类似这样的课程设置:

<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

我需要在客户端系统上使用这些类,还需要能够通过管理界面创建这些“任务”。继承基类的每个类(任务)都可以有自己的属性,这些属性对每个类都是唯一的,但同时共享相同的公共基类属性。例如,上面显示了一个重新启动任务和一个删除文件任务,删除文件任务需要知道要删除哪个文件,因此有一个属性。但是重启任务不需要这个属性。因此,当管理应用程序创建这些任务时,它不应该为重新启动任务的文件属性提供文本框。以后可能会创建更多具有完全不同属性的任务。

例如,我将如何为 WinForms 管理应用程序提供一种将每个类枚举到 ListView 中的方法,并允许用户创建这些任务并填写每个类将具有的动态属性?

所需的功能是创建一个任务表单,根据每个类中的公共属性,根据需要创建可用于属性的动态控件,但同时还具有可用的基类属性。

感谢任何想法。

谢谢, 斯科特

【问题讨论】:

    标签: c# vb.net class abstract-class


    【解决方案1】:

    如果您需要高度可定制和可扩展的解决方案,那么一种方法就是日期驱动方法。在我之前的一项工作中,我们必须根据客户偏好创建一个动态 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 字段创建类,并将创建绑定关联到生成的控件。

    这很复杂,但您获得的好处是您可以关闭/打开控件以显示。

    编辑: 如果您打算使用属性方法,以下可能会很方便。根据您的示例,您已经将所需的属性标记为 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()
    

    【讨论】:

    • 感谢您的回复。我现在真的想把它放在数据库之外。在发布我的问题后,我开始阅读类和字段的自定义属性,然后能够读取这些标志值以确定该字段是否应该为其创建控件。我认为它本质上与您建议的相同,只是消除了对数据库的需求。
    • @ScottN。请参阅编辑以使用反射来获取所需的属性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-17
    • 2011-04-14
    • 1970-01-01
    • 2011-02-21
    • 1970-01-01
    • 1970-01-01
    • 2020-03-08
    相关资源
    最近更新 更多