将扩展方法应用于具有泛型类型的泛型类

发布于 2024-07-10 02:58:22 字数 629 浏览 5 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风渺 2024-07-17 02:58:22

如果您将扩展方法设为通用方法,它应该可以工作

,即 DoSomething (Of T) () 而不是 DoSomething()

<Extension()> _
Public Sub DoSomething(Of T)(ByVal myGenericObj As MyGeneric(Of T))
End Sub

希望这会有所帮助

If you make your extension method a generic method it should work

i.e DoSomething (Of T) () instread of just DoSomething()

<Extension()> _
Public Sub DoSomething(Of T)(ByVal myGenericObj As MyGeneric(Of T))
End Sub

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文