简体   繁体   English

在GUI中使用许多从基类继承的类

[英]Using many classes in GUI that inherit from a base

I have classes setup similar to this: 我有与此类似的类设置:

<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

I need to use these classes on the client system, but also need to be able to create these "tasks" through a management interface. 我需要在客户端系统上使用这些类,但是还需要能够通过管理界面创建这些“任务”。 Each class (Task) that inherits the base, could have its own properties that are unique to each class, but at the same time, share the same common base class properties. 继承基类的每个类(任务)可以具有自己的属性,这些属性对于每个类都是唯一的,但是同时,它们共享相同的公共基类属性。 For example, the above shows a reboot task and a delete file task, the delete file task needs to know which file to delete, so has a property for that. 例如,上面显示了重新引导任务和删除文件任务,删除文件任务需要知道要删除哪个文件,因此具有相应的属性。 But the reboot task does not need this property. 但是重新启动任务不需要此属性。 So when the management application is creating these tasks, it shouldn't provide a text box for the file property for the reboot task. 因此,当管理应用程序创建这些任务时,不应为重新启动任务的file属性提供文本框。 There may be more tasks created at a later date with completely different properties. 以后可能会创建更多具有完全不同属性的任务。

How would I go about providing the WinForms management application a way to enumerate each class into a ListView for example, and allowing the user to create these tasks and filling in the dynamic properties that each class would have? 我将如何为WinForms管理应用程序提供一种将每个类枚举到ListView中的方法,并允许用户创建这些任务并填写每个类将具有的动态属性?

Desired functionality would be to create a task form that creates dynamic controls available for the properties as needed, depending on the public properties in each class, but at the same time have the base class properties available as well. 所需的功能将是创建一个任务表单,该表单根据每个类中的公共属性来创建可用于该属性的动态控件,但同时也可以使用基类属性。

Any ideas are appreciated. 任何想法表示赞赏。

Thanks, Scott 谢谢,斯科特

If you need a highly customizable and extendable solution, then one way to go about doing it would be date driven approach. 如果您需要高度可定制和可扩展的解决方案,那么执行此操作的一种方法是日期驱动方法。 In one of my previous job, we had to create a dynamic UI based on client preference where field 1 was applicable to one client, but the same was not needed by another. 在我以前的工作之一中,我们必须基于客户端首选项创建一个动态UI,其中字段1适用于一个客户端,而另一客户端则不需要。 Extending that approach to your question, you will need to have DB tables 将这种方法扩展到您的问题,您将需要数据库表

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

Another table with the actual fields for the tasks. 另一个表包含任务的实际字段。 Lets call it TaskFields for now. 现在将其称为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)

These are fairly small tables. 这些是很小的表。 Load them up when the app starts. 在应用启动时将其加载。 In your app, bind your ListBox to the TaskName field in TasksTable. 在您的应用程序中,将ListBox绑定到TasksTable中的TaskName字段。 When a specific item is selected, dynamically create controls based on the values in TaskFields table for the selected Item. 选择特定项目后,根据TaskFields表中所选项目的值动态创建控件。 Create the class based on the ClassName field and associate create bindings to the controls generated. 基于ClassName字段创建类,然后将创建绑定与所生成的控件相关联。

This is complex, but the advantage you get is you can switch Off/On controls to show up. 这很复杂,但是您获得的好处是可以关闭/打开控件以显示。

EDIT: If you plan to use the attributes approach, following might be handy. 编辑:如果您打算使用属性方法,以下可能很方便。 Based on your examples, you are already marking the properties you need as Datamember. 根据示例,您已经将所需的属性标记为数据成员。 So you don't have to define any custom attribute, unless you have other properties which will not be used for user input but have themselves marked DataMember for other purposes. 因此,您不必定义任何自定义属性,除非您具有其他将不用于用户输入的属性,但将其自身标记为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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从抽象基类多重继承? - How to mulitiply inherit from abstract base classes? 使用Salesforce作为WCF服务的客户端有什么涉及,WCF服务的DataContract包含从基类继承的类? - What's involved with using Salesforce as a client to a WCF service that has a DataContract that contains classes that inherit from a base class? 如何使用XmlSerializer为从基类继承的类的节点名编写XmlType - How to write XmlType for node names of classes that inherit from a base class using XmlSerializer 使用 Redis 和 C# 存储从基类继承的类的集合 - Storing collection of classes that inherit from a base class using Redis & C# 你可以在C#中继承多少个类? - How many classes can you inherit from in C#? Linq将许多表导入具有嵌套类的继承类 - Linq result from many tables into an inherit class with nested classes 从基类继承 - inherit from a base class 试图继承三个基类,不能 - Trying to inherit three base classes and can't 对我从继承的类引用的对象使用属性是否有优势 - Is there an advantage to using properties for objects I reference from classes that inherit 如何编写通用方法来复制均从同一抽象基类继承的类的实例? - How do I write a general method to copy instances of classes that all inherit from the same abstract base class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM