Extension members not for type 'Object'?

  • Thread starter Thread starter Armin Zingler
  • Start date Start date
A

Armin Zingler

Hi,

why do Extensions not work for references of type 'Object'?
It's only available for all other types. One (huge) example:

<Extension()> _
Public Function Cast(Of TResult)(ByVal var As Object) As TResult
Return DirectCast(var, TResult)
End Function

But:
Dim c As Control = New Form
Dim o As Object = c
Dim f As Form

f = c.Cast(Of Form)() 'works
f = o.Cast(Of Form)() '-----> does not work <------

Is there a reason for this limitation that I'm unable to see?
 
Armin Zingler said:
Hi,

why do Extensions not work for references of type 'Object'?
It's only available for all other types. One (huge) example:

<Extension()> _
Public Function Cast(Of TResult)(ByVal var As Object) As TResult
Return DirectCast(var, TResult)
End Function

But:
Dim c As Control = New Form
Dim o As Object = c
Dim f As Form

f = c.Cast(Of Form)() 'works
f = o.Cast(Of Form)() '-----> does not work <------

Is there a reason for this limitation that I'm unable to see?

This is, apparently, a known problem...

http://blogs.msdn.com/b/vbteam/arch...nd-late-binding-extension-methods-part-4.aspx

HTH
 
Am 14.04.2011 08:43, schrieb Jason Keats:


Jason, thanks for the link! (I wonder how you found it) Exactly what I was
looking for. Unfortunately the VB developers (the MSFT people) always seem
to avoid the introduction of options that control the compiler behavior. It
should be up to the programmer if it's an issue in an early bound scenario.
The option could enable the usage of the type Object with extension members.
Several times I wished there were options in other situations, too. Well,
*sigh* it's only VB. (and if we didn't need Modules for extension methods,
I'd be glad, too... *sigh*)
 
Back
Top