大佬教程收集整理的这篇文章主要介绍了VB.NET 通用接口、基派生类和工厂模式,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
嗨,我在为通用接口创建工厂模式时遇到问题。
我有 IfileHelper(Of T) 接口:
Public interface IfileHelper(Of T)
Property Containsheader As @R_801_8487@an
Function IsValID(path As String) As fileValIDationResult
Function Readfile(path As String) As List(Of T)
End Interface
我有 CsvfileHelper(Of T),它实现了接口 IfileHelper(Of T)
Public Class CsvfileHelper(Of T)
Implements IfileHelper(Of T)
Public Property Containsheader As @R_801_8487@an Implements IfileHelper(Of T).Containsheader
Public Function IsValID(path As String) As @R_801_8487@an Implements IfileHelper(Of T).IsValID
End Function
Public Function Readfile(path As String) As List(Of T) Implements IfileHelper(Of T).Readfile
End Function
End Class
现在我有一个名为 fileModel 的抽象类。
Public MusTinherit Class fileModel
End Class
以及其他继承抽象类的模型类:
Public Class CarModel
inherits fileModel
'fIElds and propetIEs
End Class
Public Class Truckmodel
inherits fileModel
'fIElds and propertIEs
End Class
Public Shared Function fileFactory(Of fileModel)(ByVal vehicle As VehicleTypE) As IfileHelper(Of fileModel)
SELEct Case vehicle
Case VehicleType.Car
Return CType(New CsvfileHelper(Of CarModel),IfileHelper(Of fileModel))
Case VehicleType.Truck
Return CType(New CsvfileHelper(Of Truckmodel),IfileHelper(Of fileModel))
Case Else
Throw New NotImplementedException("Not implemented for type " & vehicle.ToString.Trim & "!")
End SELEct
End Function
主要方法:
Public Sub Main()
Dim fileHelper As IfileHelper(Of fileModel) = fileFactory(Of fileModel)(VehicleType.Car)
End Main
我已将 Option Strict 设置为 Off 但这无济于事,有什么办法可以使这件事起作用吗?
编辑: 我编辑并使用了@jmcilhinney 在他的回答中显示的工厂方法,但出现运行时错误:
Unable to cast object of type 'CsvfileHelper`1[CarModel]' to type 'IfileHelper`1[fileModel]'
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
以上是大佬教程为你收集整理的VB.NET 通用接口、基派生类和工厂模式全部内容,希望文章能够帮你解决VB.NET 通用接口、基派生类和工厂模式所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。