Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VB.NET、模板、反射、继承、感觉飘忽不定_Vb.net_Reflection_Inheritance_Templates_.net 4.0 - Fatal编程技术网

VB.NET、模板、反射、继承、感觉飘忽不定

VB.NET、模板、反射、继承、感觉飘忽不定,vb.net,reflection,inheritance,templates,.net-4.0,Vb.net,Reflection,Inheritance,Templates,.net 4.0,我刚刚给自己编了一个问题,现在想知道如何解决它 首先,我使用了一些第三方组件,包括一些日历控件,如日程表和时间线。它们在项目类中的使用或多或少是这样的: Friend Class TimeBasedDataView 'some members End Class Friend Class ScheduleDataView Inherits TimeBasedDataView Public Schedule As Controls.Schedule.Schedule

我刚刚给自己编了一个问题,现在想知道如何解决它

首先,我使用了一些第三方组件,包括一些日历控件,如日程表和时间线。它们在项目类中的使用或多或少是这样的:

Friend Class TimeBasedDataView
    'some members
End Class

Friend Class ScheduleDataView
    Inherits TimeBasedDataView

    Public Schedule As Controls.Schedule.Schedule
    'and others
End Class

Friend Class TimeLineDataView
    Inherits TimeBasedDataView

    Public TimeLine As Controls.TimeLine.TimeLine
    'and others
End Class
(嗯,代码着色失败,没关系…)现在,为了管理呈现的数据的外观,有一些机制,包括所谓的样式管理器。它们中的许多代码重复,几乎只随其维护的控件而变化:

Friend Class TimeLineStyleManager
    Private m_TimeLine As TimeLineDataView

    Private Sub Whatever()
        m_TimeLine.TimeLine.SomeProperty = SomeValue
    End Sub
End Class

Friend Class ScheduleStyleManager
    Private m_Schedule As ScheduleDataView

    Private Sub Whatever()
        m_Schedule.Schedule.SomeProperty = SomeValue
    End Sub
End Class
我想知道我是否可以为那些经理创建一些基类,比如

Friend Class TimeBasedCtrlStyleManagerBase(Of T As TimeBasedDataView)
    Private m_Control As T
    'and others
End Class

这将统一这两个组件,但在维护两个没有共同点的组件(除了它们的属性名称等)时,我迷失了方向。也许是类型反射?如有任何建议,我将不胜感激;)

看起来您已经有了一个案例,您希望引入继承,而这是不需要的-如果第三方控件都遵循一个公共接口(然后泛型可能就省去了这一天),那会更好,但是因为它们是第三方,我假设您对it的未来方向的影响最小。

您做的事情完全正确—继承和泛型非常适合这种情况—但我会让
TimeBasedCrlStyleManagerBase
类必须继承/抽象,然后简单地继承您的两个特定管理器

所有通用的管理代码都放在抽象基类中,任何特定的代码都放在两个特定的管理器中

当然,您需要将
Private m_Control As T
更改为
Protected m_Control As T
,这样才能工作

Friend Class TimeLineStyleManager
    Inherits TimeBasedCtrlStyleManagerBase(Of TimeLineDataView)
End Class

Friend Class ScheduleStyleManager
    Inherits TimeBasedCtrlStyleManagerBase(Of ScheduleDataView)
End Class

是的,我已经弄明白了(必须继承和保护)。我的问题是如何使基类使用适当的内部控件(m_control.TimeLine或m_control.Schedule),或者,例如,在某些情况下,使用适当名称空间的枚举(如Controls.Schedule.TriState)。我知道这听起来很混乱,但这就是我的全部观点,因为一个管理器具有循环:“对于每个MyFormat作为TimeLine.TimeLineFormatCondition[…]”和另一个:“对于每个MyFormat作为Schedule.ScheduleFormatCondition[…]“里面有一些日程/时间线类型的变量,但主体本身是相同的。@brovar-没有那么凌乱-只是普通的东西都在基本泛型类中,您提到的所有特定内容都在派生的管理器类中。这就是它的工作原理。如果您有任何进一步的问题,可以发布一个新的问题(在这里的评论中有一个链接),或者为这个问题添加更多的细节?