情景
我编写了这些扩展方法,以优雅/简化的方式初始化和/或处理Array的所有元素,以避免代码重复:
<HideModuleName> Public Module ArrayExtensions <Extension> Public Sub InitializeAll(Of T As New)(ByVal sender As T()) For index As Integer = 0 To (sender.Length - 1) sender(index) = New T Next index End Sub <Extension> Public Sub InitializeAll(Of T As Idisposable)(ByVal sender As T()) ArrayExtensions.disposeAll(sender) For index As Integer = 0 To (sender.Length - 1) sender(index) = Activator.CreateInstance(Of T)() Next index End Sub <Extension> Public Sub disposeAll(Of T As Idisposable)(ByVal sender As T()) For index As Integer = 0 To (sender.Length - 1) If (sender(index) IsNot nothing) Then sender(index).dispose() sender(index) = nothing End If Next index End Sub End Module
目的是在这样的常见场景中使用它:
Dim myCollection As MydisposableType() = New MydisposableType(100) {} myCollection.InitializeAll() myCollection.disposeAll()
问题
‘Public Sub InitializeAll(Of T As New)(sender() As T)’ has multiple
deFinitions with identical signatures.
…我真的不明白这个错误,因为Idisposable是一个接口,我试图删除类型参数的“As New”但仍然显示相同的错误.
题
我如何调整InitializeAll()方法的类型参数以避免此错误并仍然在预期的行为中工作?我的意思是一个方法接受一次性objetcs数组,另一个方法接受非一次性对象.
<Extension> Public Sub InitializeAll(Of T As New)(ByVal sender As T()) For index As Integer = 0 To (sender.Length - 1) If (sender(index) IsNot nothing) Then If sender(index).GetType.GetInterfaces.Contains(GetType(Idisposable)) Then DirectCast(sender(index),Idisposable).dispose() sender(index) = nothing End If End If sender(index) = New T Next index End Sub
解决方法
这里真正的问题是当一个对象有一个Constructor并且也是Idisposable时会发生什么?请问哪种方法?这就是它导致问题的原因.
通用类型不能用于重载,因为编译器无法在编译时区分两种不同类型,因此认为它们相同…
所以你要么创建一个真正的Overload,比如:
'Would mean you can dispose them,but you have to load the method with Reflection Public Overload Sub InitializeAll(of T As New)(ByVal Sender As T(),ByVal CollectionIsdisposable As Boolean) 'So here they are not disposable... Public Overloads Sub InitializeAll(of T As New(ByVal Sender As T())
在那里你得到一个真正的重载,但正如你所说,它可能会破坏你的逻辑,然后只需要一个方法,并检查对象是否实现Idisposable.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。