Extension members not for type 'Object'?

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?
 
J

Jason Keats

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
 
A

Armin Zingler

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*)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top