将扩展方法应用于具有泛型类型的泛型类
我正在使用 vb.net 中的通用类。
而且如果不指定类型,扩展方法似乎无法应用于泛型类。
我有这个泛型类
Public Class MyGeneric(Of T)
'Methods and properties go here
'
'
End Class
这是好的
<Extension()> _
Public Sub DoSomething(ByVal myGenericDoubleObj As MyGen(Of Double))
End Sub
这是不好的(IDE 给我错误 T 未定义。)
<Extension()> _
Public Sub DoSomethingGeneric(ByVal myGenericObj As MyGen(Of T))
End Sub
这与 .Net 的静态检查有关吗?
对我说“你尝试用 T 型做的事情可能不兼容,我不会允许你这样做。”
PS 我作为通用类所承受的所有痛苦都来自另一个库,并且在许多不同的地方使用。 我对在继承的泛型类中继承和添加此方法不太谨慎。
I was working with the generic class in vb.net.
And it seems extension method cannot be applied to generic class without specifying the type.
I have this generic class
Public Class MyGeneric(Of T)
'Methods and properties go here
'
'
End Class
This is Ok
<Extension()> _
Public Sub DoSomething(ByVal myGenericDoubleObj As MyGen(Of Double))
End Sub
This is NOT Ok(IDE gives me error T as not defined.)
<Extension()> _
Public Sub DoSomethingGeneric(ByVal myGenericObj As MyGen(Of T))
End Sub
Is this something to do with the static checking of the .Net.
Saying me "Something which may you try with doing with Type T is may not compatible and I will not allow you to do it."
P.S. All the this pain I have taken as Generic Class comes from another library, And used at many different places. I am little wary of inheriting and adding this method in my inherited generic class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您将扩展方法设为通用方法,它应该可以工作
,即 DoSomething (Of T) () 而不是 DoSomething()
希望这会有所帮助
If you make your extension method a generic method it should work
i.e DoSomething (Of T) () instread of just DoSomething()
Hope this helps