通过后期绑定在VB.NET中进行多态处理无法使用事件,是否可以解决? [英] Polymorphism in VB.NET via Late Binding disallows With Events, workaround?

查看:103
本文介绍了通过后期绑定在VB.NET中进行多态处理无法使用事件,是否可以解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发与USB传感器家族对话的应用程序.我创建了一个基本的实现,该实现使用了一个称为Sensor的类.该类包含允许与传感器交互的事件和方法(也涉及线程任务处理器,但我将举一个简单的示例).

I'm working on developing an application that talks to a family of USB sensors. I've created a basic implementation that utilizes a class called Sensor. The class contains events and methods that allow for interaction with the sensor (there is also a threaded task processor involved but I'll go with a simple example).

我的问题是,这个简单的概念证明示例可以很好地工作,但是现在我需要扩展应用程序以支持整个传感器系列.为此,我创建了具有所有适当方法和事件的BaseSensor类,然后创建了所有固有BaseSensor的多个子类,例如SensorA,SensorB和SensorC.

My issue is that this simple proof of concept example works fine but now I need to expand the application to support the whole family of sensors. To do this I've created a BaseSensor class with all the appropriate methods and events and then I've created multiple subclasses such as SensorA, SensorB, and SensorC that all inherent BaseSensor.

这似乎是多态性的良好应用,因此我在BaseSensor上创建了一个名为Initialize的共享函数,该函数执行初始USB通信并根据传感器类型(SensorA,SensorB,SensorC)返回正确的对象.这个工作正常,但是似乎我找不到正确声明带有事件的对象的方法.请参阅示例代码以了解我的习惯.

This seemed like a good application of polymorphism so I've created a Shared function on BaseSensor called Initialize that does an initial USB communication and returns the correct object depending on the sensor type (SensorA, SensorB, SensorC). This works fine however it seems I can not find a way to correctly declare the object With Events. See the sample code for my deliema.

尝试1:

Public Class Form1
    Dim WithEvents oBaseClass As BaseClass
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        oBaseClass = New ExtendedClass
        oBaseClass.Test() 'This doesn't work because the object was type casted.
    End Sub

    Private Sub TestEventHdlr() Handles oBaseClass.TestEvent
        MsgBox("Event Fired")
    End Sub
End Class

Public Class BaseClass
    Public Event TestEvent()
End Class

Public Class ExtendedClass
    Inherits BaseClass
    Public Sub Test()
        MsgBox("Test")
    End Sub
End Class

尝试2:

Public Class Form1
    Dim WithEvents oBaseClass 'This doesn't work.
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        oBaseClass = New ExtendedClass
        oBaseClass.Test()
    End Sub

    Private Sub TestEventHdlr() Handles oBaseClass.TestEvent
        MsgBox("Event Fired")
    End Sub
End Class

Public Class BaseClass
    Public Event TestEvent()
End Class

Public Class ExtendedClass
    Inherits BaseClass
    Public Sub Test()
        MsgBox("Test")
    End Sub
End Class

我在这里丢失了一些东西.我应该如何进行?

I'm missing something here. How should I proceed?

推荐答案

WithEvents不能被后期绑定.您需要使用类型声明字段.如果此方案中使用的所有对象都来自一个共同的基础,那么您将在放弃后期绑定方面为自己提供巨大的帮助.必要时进行强制转换,并声明虚拟(可重写)方法来实现您的多态性.

WithEvents can not be late bound. You need to declare your field with a type. If all objects used in this scenario derive from a common base, you will be doing yourself a huge favor in ditching late binding. Cast when necessary, and declare virtual (overridable) methods to implement your polymorphism.

Public Class Form1
    Dim WithEvents oBaseClass As BaseClass 'Early bound'

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        oBaseClass = New ExtendedClass
        DirectCast(oBaseClass, ExtendedClass).Test() 'Casting to call a method'
    End Sub

    Private Sub TestEventHdlr() Handles oBaseClass.TestEvent
        MsgBox("Event Fired")
    End Sub
End Class

Public Class BaseClass
    Public Event TestEvent()
End Class

Public Class ExtendedClass
    Inherits BaseClass
    Public Sub Test()
        MsgBox("Test")
    End Sub
End Class

这篇关于通过后期绑定在VB.NET中进行多态处理无法使用事件,是否可以解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆