应用扩展方法的泛型类与泛型类型 [英] Applying Extension method to generic class with generic type

查看:148
本文介绍了应用扩展方法的泛型类与泛型类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与在vb.net中的泛型类。
它似乎扩展方法不能被应用到通用类而不指定型

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.

我有这样的泛型类

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作为没有定义。)

This is NOT Ok(IDE gives me error T as not defined.)

<Extension()> _
Public Sub DoSomethingGeneric(ByVal myGenericObj As MyGen(Of T))

End Sub

这是不是做与净的静态检查。
说我有什么可尝试与T型做的是可能不兼容,我不会让你做到这一点。

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。所有这种痛苦我已经为通用类来自其他库,并在许多不同的地方使用。我有点担心继承和在我继承泛型类中添加此方法。

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.

推荐答案

如果你让你的扩展方法,它应该工作泛型方法

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

即DoSomething的(对T)()instread只是DoSomething的(中)

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

这篇关于应用扩展方法的泛型类与泛型类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆