I have a class library with some extension methods written in C# and an old website written in VB.
I want to call my extension methods from the VB code but they don't appear in intelisense and I get compile errors when I visit the site.
I have got all the required Imports because other classes contained in the same namespaces are appearing fine in Intelisense.
Any suggestions
EDIT: More info to help with some comments.
my implementation looks like this
//C# code compiled as DLL
namespace x.y {
public static class z {
public static string q (this string s){
return s + " " + s;
}
}
}
and my usage like this
Imports x.y
'...'
Dim r as string = "greg"
Dim s as string = r.q() ' does not show in intelisense
' and throws error : Compiler Error Message: BC30203: Identifier expected.
This was quite a while ago and I can't really how I solved it, but needless to say, it was user error. I probably restarted my computer and away it went.
Extension methods are just syntactic sugar for static methods. So
can be called in both VB.NET and C# with
I ran into the same problem, and might accidentally have stumbled upon the solution. If I used
, it threw the same error for me as well. But if I imported x.y, it worked, so:
was fine.
So apparently you have to import it in the declaration to make it work.
Does “old” here imply perhaps that you also use an old version of VB here? Anyway, since extension methods are just vanilla static (“
Shared
”) methods decorated with an attribute, you should be able to call them in any case.If this isn't possible you either try to call them “extension style” in an old version of VB or you're referencing the wrong version of your C# assembly.
Edit: are you sure you're
Import
ing the whole namespace, i.e.x.y
and not justx
? VB is able to access nested namespaces easier than C# so you can use classes from namespacex.y
using the following code in VB. However, for extension methods to work, the full path has to beImport
ed.I don't know if you can call them in the same dot notation as you would in C#, but I would think that the static extension methods would show up as static functions with the fist argument as the extended type. So you should be able to call the actually class in VB with:
Where in C# you would have just written:
With StaticClass being the name of the static class in which you defined your extension methods.
I think I've encountered a similar problem: VB.Net is quite happy to compile on through extension methods and leave them to be inferred at run-time if
Option Strict
is off.However, VB.Net really doesn't seem to like extension methods on basic types. You can't extend
Object
and it can't resolve it if you do:C#
However this goes wrong in VB.Net:
That compiles without error, but at run time fails because
Something
is not a method ofString
- the compiler has failed to replace the syntactic sugar of the extension method with the the static call toUtility.Something
.The question is why? Well unlike C#, VB.Net can't handle any extension to
Object
! The valid extension method in C# confuses the VB.Net compiler.As a general VB.Net rule, I'd avoid using extension methods with any of the basic .Net types (
Object
,String
,Integer
, etc). You also have to be careful withOption Infer
as while it's on by default in Visual Studio it's off by default for command line compiles,VBCodeProvider
, and possibly in web sites (depending on your web.config). When it's off everything in VB.Net is considered to be anObject
and all extension methods will be left until run time (and will therefore fail).I think Microsoft really dropped the ball when they added extension methods to VB.Net, I think it was an afterthought to try (and fail) to make it consistent with C#.