我如何可以调用VB代码C#扩展方法 [英] How can I call C# extension methods in VB code

查看:145
本文介绍了我如何可以调用VB代码C#扩展方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类库用C#编写,并用VB写的一个旧网站上的一些推广方法。



我想打电话给从VB代码我的扩展方法,但它们不会出现在智能感知和我得到的,当我访问该网站编译错误。



我已经得到了所有的必需的导入的是因为其他,包含在同一个命名空间中的类出现罚款,智能感知



任何建议



编辑:更多信息以帮助一些意见。



我的实现看起来像这样

  // C#代码编译成DLL 
命名空间XY {
公共静态类Z,{
公共静态字符串q(这个字符串s){
返回S + + S;
}

}
}

和我的使用这样

 导入XY 

'...'
暗淡 - [R作为字符串=格雷格
尺寸参考译文]字符串= RQ()不智能感知
显示,并抛出错误:编译器错误消息:BC30203:标识符。


解决方案

这对我的作品,虽然有一对夫妇怪癖。首先,我创建了一个C#类库针对.NET 3.5。下面是该项目的唯一代码:

 使用系统; 

命名空间ExtensionLibrary
{
公共静态类扩展
{
公共静态字符串CustomExtension(此字符串文本)
{
的char [] =字符text.ToCharArray();
Array.Reverse(字符);
返回新的字符串(字符);
}
}
}



然后,我创建了一个VB控制台应用定位.NET 3.5,并添加到我的C#项目的引用。我改名为的Module1.vb Test.vb,这里是代码:

 进口ExtensionLibrary 

模块测试

子的Main()
Console.WriteLine(你好.CustomExtension())
端子

端模块

这编译和运行。 (我会调用的方法反向(),但我不知道是否会VB神奇逆转有能力已经某处 - 我不是一个长粉笔VB专家)



起初,我不提供ExtensionLibrary从智能感知进口。即使建成后,将进口ExtensionLibrary是灰色的,和一个灯泡提供了机会,去除多余的所谓进口。 (这样做打破了该项目。)这是可能的,这是ReSharper的,而不是Visual Studio中,请注意。



因此,为了削减长话短说,是可以做到的,它应该只是罚款。我不认为这个问题是,你要么使用的是旧版本的VB或您的项目是不是针对.NET 3.5?


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.

解决方案

It works for me, although there are a couple of quirks. First, I created a C# class library targeting .NET 3.5. Here's the only code in the project:

using System;

namespace ExtensionLibrary
{
  public static class Extensions
  {
    public static string CustomExtension(this string text)
    {
      char[] chars = text.ToCharArray();
      Array.Reverse(chars);
      return new string(chars);
    }
  }
}

Then I created a VB console app targeting .NET 3.5, and added a reference to my C# project. I renamed Module1.vb to Test.vb, and here's the code:

Imports ExtensionLibrary

Module Test

    Sub Main()
        Console.WriteLine("Hello".CustomExtension())
    End Sub

End Module

This compiles and runs. (I would have called the method Reverse() but I wasn't sure whether VB might magically have reverse abilities already somewhere - I'm not a VB expert by a long chalk.)

Initially, I wasn't offered ExtensionLibrary as an import from Intellisense. Even after building, the "Imports ExtensionLibrary" is greyed out, and a lightbulb offers the opportunity to remove the supposedly redundant import. (Doing so breaks the project.) It's possible that this is ReSharper rather than Visual Studio, mind you.

So to cut a long story short, it can be done, and it should work just fine. I don't suppose the problem is that you're either using an old version of VB or your project isn't targeting .NET 3.5?

这篇关于我如何可以调用VB代码C#扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆