在同一个应用程序中使用 VB.NET 和 C#?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/862723/
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
Use VB.NET and C# in the same application?
提问by Amit Kumar Jha
I am developing a GUI based application in MS Visual Studio 2005, I just want to know if it is possible to use both VB.NET and C# in the same project. Or can I include a module written in C# in my VB.NET project?
我正在 MS Visual Studio 2005 中开发基于 GUI 的应用程序,我只想知道是否可以在同一个项目中同时使用 VB.NET 和 C#。或者我可以在我的 VB.NET 项目中包含一个用 C# 编写的模块吗?
I have a class written in C# which I want to use in my VB.NET based project, so if I can include and call functions from that project than I won't have to write the class again in VB.NET.
我有一个用 C# 编写的类,我想在基于 VB.NET 的项目中使用它,所以如果我可以从该项目中包含和调用函数,那么我就不必在 VB.NET 中再次编写该类。
So please help me as I am new to .NET programming.
所以请帮助我,因为我是 .NET 编程的新手。
采纳答案by Jared Harley
I've never done it myself, but I know you can compile the C# code into a dll and then load and reference the dll in your VB project.
我自己从来没有做过,但我知道你可以将 C# 代码编译成一个 dll,然后在你的 VB 项目中加载和引用这个 dll。
From "Calling C# class in VB.net":
I think the C# code that you want to use must be compiled as a DLL. Once that is done, simple add a reference to that project to your VB.Net project, import the namespace you need, and then you can use the C# code.
我认为您要使用的 C# 代码必须编译为 DLL。完成后,简单地将对该项目的引用添加到您的 VB.Net 项目,导入您需要的命名空间,然后您就可以使用 C# 代码了。
Also see How To: Create and Use C# DLLs(from MSDN, for VS2005)
另请参阅如何:创建和使用 C# DLL(来自 MSDN,适用于 VS2005)
回答by Konrad Rudolph
I just want to know that is it possible to use both VB and C# in the same project.
我只是想知道是否可以在同一个项目中同时使用 VB 和 C#。
No, not in the same project. On the other hand, you can use them in the same solution.
不,不在同一个项目中。另一方面,您可以在同一个解决方案中使用它们。
Or can i include a module written in C# in my VB.net project.
或者我可以在我的 VB.net 项目中包含一个用 C# 编写的模块。
I propose that you create a solution containing two projects: one in C# which forms a library that you use from your VB project. This is straightforward, easy to maintain and easy to extend.
我建议您创建一个包含两个项目的解决方案:一个在 C# 中,它形成一个您从 VB 项目使用的库。这很简单,易于维护且易于扩展。
回答by Martin Harris
You also want to ensure that you C# code is CLS compliant. This means that it won't publicly expose any functionality which other .NET languages won't understand (for example unsigned ints - which don't exist in VB, or differing classes only by case - since VB is not case-sensitive). To do this you need to add an attribute so that the compiler will raise errors if you have broken any of the guidelines. This articleshows you how to do this:
您还希望确保您的 C# 代码符合 CLS。这意味着它不会公开暴露其他 .NET 语言无法理解的任何功能(例如无符号整数 - 在 VB 中不存在,或者仅按大小写不同的类 - 因为 VB 不区分大小写)。为此,您需要添加一个属性,以便在您违反任何准则时编译器会引发错误。这篇文章向您展示了如何做到这一点:
The CLSCompliantAttribute can be applied to assemblies, modules, types, and members.
For marking an entire assembly as CLS compliant the following syntax is used
using System; [assembly:CLSCompliant(true)]
For marking a particular method as CLS compliant the following syntax is used
[CLSCompliant(true)] public void MyMethod()`
CLSCompliantAttribute 可应用于程序集、模块、类型和成员。
为了将整个程序集标记为符合 CLS,使用以下语法
using System; [assembly:CLSCompliant(true)]
为了将特定方法标记为符合 CLS,使用以下语法
[CLSCompliant(true)] public void MyMethod()`
回答by Aaron M
If you were only planning on using the module in Visual Basic projects, then you should consider just converting the code to Visual Basic. If you need to use the module in both C# and VB.NETprograms I would use one of the solutions posted above
如果您只打算在 Visual Basic 项目中使用该模块,那么您应该考虑只将代码转换为 Visual Basic。如果您需要在 C# 和VB.NET程序中使用该模块,我将使用上面发布的解决方案之一
You might try something like *Convert C# to VB.NET. It converts C# to VB.NET code. I use this page exclusively when I have to convert something I find on the net that was written in C#.
你可以尝试像 * Convert C# to VB.NET这样的东西。它将 C# 转换为 VB.NET 代码。当我必须转换我在网上找到的用 C# 编写的内容时,我专门使用此页面。
回答by Peter Mortensen
Put VB.NET and C# code in separate projects. (I am using both VB.NET and C# in my open source project, http://msquant.sourceforge.net/, and it works great).
将 VB.NET 和 C# 代码放在不同的项目中。(我在我的开源项目http://msquant.sourceforge.net/ 中同时使用了 VB.NET 和 C# ,而且效果很好)。
You don't need to worry about DLLs, just reference the project(use tab "Project" in the "Add Reference" dialog box). E.g. if you need to use a function in the C# code/project add a reference in the VB.NET project to the C# project.
您无需担心 DLL,只需引用项目(使用“添加引用”对话框中的“项目”选项卡)。例如,如果您需要在 C# 代码/项目中使用函数,请在 VB.NET 项目中添加对 C# 项目的引用。
回答by Samuel
You must also know that if you have a VB.NETproject with a C#project in the same solution with one of them having a reference to the other, changes apply in the referencing project will just be available to the other after rebuilding the solution. It's like having binary reference, but with the capability to change code on the same solution.
您还必须知道,如果在同一解决方案中有一个VB.NET项目和C#项目,其中一个项目引用另一个,则在重建解决方案后,引用项目中应用的更改将仅对另一个可用。这就像拥有二进制引用,但能够在同一解决方案上更改代码。
Personally, I don't like this, but I'm always in the situation where I modify the code in the referencing project and don't know why my changes are not in the code where I use it and I figure it out, oohhhh, I must rebuild.
就我个人而言,我不喜欢这样,但我总是处于我修改引用项目中的代码的情况,不知道为什么我的更改不在我使用它的代码中,我想通了,oohhhh ,我必须重建。
For temporary help, it could be acceptable but not for programming every day.
对于临时帮助,可以接受,但不能用于每天编程。
回答by K.J.
You can't use a C# file and VB file in the same project. You can, however, have VB and C# projects in the same solution and reference them.
不能在同一个项目中使用 C# 文件和 VB 文件。但是,您可以在同一解决方案中拥有 VB 和 C# 项目并引用它们。
In your code you can use:
在您的代码中,您可以使用:
Imports namespace
or
或者
using namespace
Once the reference has been added to the appropriate project build the solution and you are good to go.
将参考添加到适当的项目后,构建解决方案,您就可以开始了。
You can also create a VB.NET Library in a separate solution, compile it and import the DLL into the C# Project or vice versa.
您还可以在单独的解决方案中创建 VB.NET 库,编译它并将 DLL 导入 C# 项目,反之亦然。