Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET: impossible to use Extension method on System.Object instance

Can I make an Extension method for all the subclasses of System.Object (everything)?

Example:

<Extension> Public Function MyExtension(value As Object) As Object     Return value End Function 

The above functions won't work for object instance:

Dim myObj1 As New Object() Dim myObj2 = myObj1.MyExtension() 

The compiler does not accept it, is the problem in my computer? :)

UPDATE
The problem seems to occur only in VB, where members of object are looked-up by reflection (late-bound).

UPDATE AFTER ANSWERED
FYI, as vb has an advantage that C# lacks that is, members of imported Modules are imported to the global scope so you can still use this functions without their wrapper:

Dim myObj2 = MyExtension(myObj1) 
like image 626
Shimmy Weitzhandler Avatar asked Jul 12 '10 11:07

Shimmy Weitzhandler


People also ask

Can you add extension methods to an existing static class?

No. Extension methods require an instance of an object.

Is it possible to achieve method extension using interface?

You can use extension methods to extend a class or interface, but not to override them. An extension method with the same name and signature as an interface or class method will never be called. At compile time, extension methods always have lower priority than instance methods defined in the type itself.

Can we override extension method?

Extension methods cannot be overridden the way classes and instance methods are. They are overridden by a slight trick in how the compiler selects which extension method to use by using "closeness" of the method to the caller via namespaces.

What is extension method in VB net?

Extension methods enable developers to add custom functionality to data types that are already defined without creating a new derived type. Extension methods make it possible to write a method that can be called as if it were an instance method of the existing type.


2 Answers

It seems like not supporting Extension methods on Object was a design decision in VB.

As a result, the only way we could prevent extension methods from completely breaking existing late bound code was to prevent them from being used on anything typed as object.

http://blogs.msdn.com/b/vbteam/archive/2007/01/24/extension-methods-and-late-binding-extension-methods-part-4.aspx

like image 117
jdot Avatar answered Oct 10 '22 02:10

jdot


See this question I asked some time ago. Basically, you can extend Object in VB.NET if you want; but for backwards compatibility reasons, no variable declared as Object will be able to use your extension method. This is because VB.NET supports late binding on Object, so an attempt to access an extension method will be ignored in favor of trying to find a method of the same name from the type of the object in question.

So take this extension method, for example:

<Extension()> Public Sub Dump(ByVal obj As Object)     Console.WriteLine(obj) End Sub 

This extension method could be used here:

' Note: here we are calling the Dump extension method on a variable ' ' typed as String, which works because String (like all classes) ' ' inherits from Object. ' Dim str As String = "Hello!" str.Dump() 

But not here:

' Here we attempt to call Dump on a variable typed as Object; but ' ' this will not work since late binding is a feature that came before ' ' extension methods. ' Dim obj As New Object obj.Dump() 

Ask yourself why extension methods don't work on dynamic variables in C#, and you'll realize the explanation is the same.

like image 43
Dan Tao Avatar answered Oct 10 '22 02:10

Dan Tao