VB.NET 中的多态性通过后期绑定不允许使用事件,解决方法? [英] Polymorphism in VB.NET via Late Binding disallows With Events, workaround?

查看:23
本文介绍了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 类,然后我创建了多个子类,例如 SensorA、SensorB 和 SensorC,它们都是固有的 BaseSensor.

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)返回正确的对象.这很好用,但似乎我找不到正确声明带有事件的对象的方法.请参阅我的 deliema 的示例代码.

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天全站免登陆