How can I call C# extension methods in VB code

2019-03-26 05:17发布

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.

10条回答
相关推荐>>
2楼-- · 2019-03-26 05:51

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.

查看更多
地球回转人心会变
3楼-- · 2019-03-26 05:54

Extension methods are just syntactic sugar for static methods. So

public static string MyExtMethod(this string s)

can be called in both VB.NET and C# with

MyExtMethod("myArgument")
查看更多
女痞
4楼-- · 2019-03-26 05:54

I ran into the same problem, and might accidentally have stumbled upon the solution. If I used

x.y.r.q()

, it threw the same error for me as well. But if I imported x.y, it worked, so:

using x.y;
...
r.q()

was fine.

So apparently you have to import it in the declaration to make it work.

查看更多
神经病院院长
5楼-- · 2019-03-26 05:59

… and an old website written in VB.

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 Importing the whole namespace, i.e. x.y and not just x? VB is able to access nested namespaces easier than C# so you can use classes from namespace x.y using the following code in VB. However, for extension methods to work, the full path has to be Imported.

Imports x
Dim x As New y.SomeClass()
查看更多
再贱就再见
6楼-- · 2019-03-26 06:00

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:

StaticClass.ExtensionMethod(theString, arg1, ..., argN)

Where in C# you would have just written:

theString.ExtensionMethod(arg1, ..., argN);

With StaticClass being the name of the static class in which you defined your extension methods.

查看更多
劫难
7楼-- · 2019-03-26 06:02

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#

namespace NS
...

public static class Utility {

    public static void Something(this object input) { ...

    public static void Something(this string input) { ...

}

// Works fine, resolves to 2nd method
"test".Something();

// At compile time C# converts the above to:
Utility.Something("test");

However this goes wrong in VB.Net:

Option Infer On
Option Explicit On
Option Strict Off
Imports NS
...

    Dim r as String = "test" 
    r.Something()

That compiles without error, but at run time fails because Something is not a method of String - the compiler has failed to replace the syntactic sugar of the extension method with the the static call to Utility.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 with Option 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 an Object 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#.

查看更多
登录 后发表回答