在同一个项目中混合使用 C# 和 VB
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1278024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mixing C# & VB In The Same Project
提问by Wes P
Can you mix vb and c# files in the same project for a class library? Is there some setting that makes it possible?
你可以在一个类库的同一个项目中混合 vb 和 c# 文件吗?是否有一些设置使其成为可能?
I tried and none of the intellisense works quite right, although the background compiler seems to handle it well enough (aside from the fact that I, then, had 2 classes in the same namespace with the same name and it didn't complain).
我尝试过,但没有一个智能感知工作得很好,尽管后台编译器似乎处理得很好(除了我在同一个命名空间中有 2 个具有相同名称的类并且它没有抱怨)。
We're trying to convert from VB to C# but haven'tfinished converting all the code. I have some new code I need to write, but didn't really want to make a new project just for it.
我们正在尝试从 VB 转换为 C#,但尚未完成所有代码的转换。我有一些需要编写的新代码,但并不真的想为此创建一个新项目。
采纳答案by Joel B Fant
No, you can't. An assembly/project (each project compiles to 1 assembly usually) has to be one language. However, you can use multiple assemblies, and each can be coded in a different language because they are all compiled to MSIL.
不,你不能。程序集/项目(每个项目通常编译为 1 个程序集)必须是一种语言。但是,您可以使用多个程序集,每个程序集都可以用不同的语言编码,因为它们都编译为 MSIL。
It compiled fine and didn't complain because a VB.NET project will only actually compile the .vb files and a C# project will only actually compile the .cs files. It was ignoring the other ones, therefore you did not receive errors.
它编译得很好并且没有抱怨,因为 VB.NET 项目实际上只会编译 .vb 文件,而 C# 项目只会实际编译 .cs 文件。它忽略了其他的,因此您没有收到错误。
Edit:If you add a .vb file to a C# project, select the file in the Solution Explorer panel and then look at the Properties panel, you'll notice that the Build Action is 'Content', not 'Compile'. It is treated as a simple text file and doesn't even get embedded in the compiled assembly as a binary resource.
编辑:如果将 .vb 文件添加到 C# 项目,请在“解决方案资源管理器”面板中选择该文件,然后查看“属性”面板,您会注意到构建操作是“内容”,而不是“编译”。它被视为一个简单的文本文件,甚至不会作为二进制资源嵌入到已编译的程序集中。
Edit:With asp.net websites you may add c# web user control to vb.net website
编辑:使用 asp.net 网站,您可以将 c# web 用户控件添加到 vb.net 网站
回答by rein
I don't see how you can compile a project with the C# compiler (or the VB compiler) and not have it balk at the wrong language for the compiler.
我不知道如何使用 C# 编译器(或 VB 编译器)编译项目,并且不会因为编译器使用错误的语言而犹豫不决。
Keep your C# code in a separate project from your VB project. You can include these projects into the same solution.
将 C# 代码保存在与 VB 项目不同的项目中。您可以将这些项目包含在同一个解决方案中。
回答by brendan
You can not mix vb and c# within the same project - if you notice in visual studio the project files are either .vbproj or .csproj. You can within a solution - have 1 proj in vb and 1 in c#.
您不能在同一个项目中混合使用 vb 和 c# - 如果您注意到 Visual Studio 中的项目文件是 .vbproj 或 .csproj。您可以在解决方案中 - 在 vb 中有 1 个 proj,在 c# 中有 1 个。
Looks like according to this you can potentially use them both in a web project in the App_Code directory:
看起来根据this您可以在App_Code目录中的Web项目中同时使用它们:
http://pietschsoft.com/post/2006/03/30/ASPNET-20-Use-VBNET-and-C-within-the-App_Code-folder.aspx
http://pietschsoft.com/post/2006/03/30/ASPNET-20-Use-VBNET-and-C-within-the-App_Code-folder.aspx
回答by svinto
You need one project per language. I'm quite confident I saw a tool that merged assemblies, if you find that tool you should be good to go. If you need to use both languages in the same class, you should be able to write half of it in say VB.net and then write the rest in C# by inheriting the VB.net class.
每种语言需要一个项目。我很自信我看到了一个合并程序集的工具,如果你找到那个工具你应该很高兴。如果你需要在同一个类中使用两种语言,你应该能够在 VB.net 中编写它的一半,然后通过继承 VB.net 类在 C# 中编写其余部分。
回答by Scott Whitlock
As others have said, you can't put both in one project. However, if you just have a small piece of C# or VB code that you want to include in a project in the other language, there are automatic conversion tools. They're not perfect, but they do most things pretty well. Also, SharpDevelophas a conversion utility built in.
正如其他人所说,您不能将两者放在一个项目中。但是,如果您只想将一小段 C# 或 VB 代码包含在其他语言的项目中,则可以使用自动转换工具。他们并不完美,但他们在大多数事情上都做得很好。此外,SharpDevelop具有内置的转换实用程序。
回答by Adam Robinson
At the risk of echoing every other answer, no, you cannot mix them in the same project.
冒着重复所有其他答案的风险,不,您不能将它们混合在同一个项目中。
That aside, if you just finished converting VB to C#, why would you write new code in VB?
除此之外,如果您刚刚完成将 VB 转换为 C#,为什么要在 VB 中编写新代码?
回答by Scott Weinstein
It might be possible with some custom MSBuild development. The supplied .targets force the projects to be single language - but there's no runtime or tooling restriction preventing this.
一些自定义 MSBuild 开发可能是可能的。提供的 .targets 强制项目为单一语言 - 但没有运行时或工具限制阻止这一点。
Both the VB and CS compilers can output to modules- the CLR's version of .obj files. Using the assembly linker, you could take the modules from the VB and CS code and produce a single assembly.
VB 和 CS 编译器都可以输出到模块- .obj 文件的 CLR 版本。使用程序集链接器,您可以从 VB 和 CS 代码中获取模块并生成单个程序集。
Not that this would be a trival effort, but it probably would work.
并不是说这将是一项微不足道的努力,但它可能会奏效。
回答by Kris Erickson
Although Visual Studio does not support this (you can do some tricks and get MSBuild to compile both, but not from within Visual Studio), SharpDevelopdoes. You can have both in the same solution (as long as you are running Visual Studio Professional and above), so the easiest solution if you want to keep using Visual Studio is to seperate your VB code into a different project and access it that way.
虽然 Visual Studio 不支持这个(你可以做一些技巧并让 MSBuild 编译两者,但不能从 Visual Studio 内部),SharpDevelop 支持。您可以在同一个解决方案中同时拥有两者(只要您运行的是 Visual Studio Professional 及更高版本),因此,如果您想继续使用 Visual Studio,最简单的解决方案是将您的 VB 代码分离到不同的项目中并以这种方式访问它。
回答by mgbeers
For .net 2.0 this works. It DOES compile both in the same project if you create sub directories of in app code with the related language code. As of yet, I am looking for whether this should work in 3.5 or not though.
对于 .net 2.0,这是有效的。如果您使用相关语言代码创建应用程序代码的子目录,它会在同一个项目中编译两者。到目前为止,我正在寻找这是否应该在 3.5 中工作。
回答by Lars Strange
Well, actually I inherited a project some years ago from a colleague who had decided to mix VB and C# webforms within the same project. That worked but is far from fun to maintain.
嗯,实际上我几年前从一位同事那里继承了一个项目,他决定在同一个项目中混合使用 VB 和 C# webforms。这有效,但维护起来并不有趣。
I decided that new code should be C# classes and to get them to work I had to add a subnode to the compilation part of web.config
我决定新代码应该是 C# 类,为了让它们工作,我必须向 web.config 的编译部分添加一个子节点
<codeSubDirectories>
<add directoryName="VB"/>
<add directoryName="CS"/>
</codeSubDirectories>
The all VB code goes into a subfolder in the App_Code called VB and the C# code into the CS subfolder. This will produce two .dll files. It works, but code is compiled in the same order as listed in "codeSubDirectories" and therefore i.e Interfaces should be in the VB folder if used in both C# and VB.
所有 VB 代码进入 App_Code 中名为 VB 的子文件夹,C# 代码进入 CS 子文件夹。这将生成两个 .dll 文件。它可以工作,但代码的编译顺序与“codeSubDirectories”中列出的顺序相同,因此如果在 C# 和 VB 中使用,即接口应位于 VB 文件夹中。
I have both a reference to a VB and a C# compiler in
我同时引用了 VB 和 C# 编译器
<system.codedom>
<compilers>
The project is currently updated to framework 3.5 and it still works (but still no fun to maintain..)
该项目目前已更新到框架 3.5,它仍然有效(但仍然没有乐趣维护......)