简体   繁体   English

在同一个应用程序中使用VB.NET和C#?

[英]Use VB.NET and C# in the same application?

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. 我在MS Visual Studio 2005中开发基于GUI的应用程序,我只想知道是否可以在同一个项目中同时使用VB.NET和C#。 Or can I include a module written in C# in my VB.NET project? 或者我可以在我的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编程的新手。

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. 我建议您创建一个包含两个项目的解决方案:一个在C#中,它构成了一个从VB项目中使用的库。 This is straightforward, easy to maintain and easy to extend. 这很简单,易于维护且易于扩展。

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" : “在VB.net中调用C#类”

I think the C# code that you want to use must be compiled as a DLL. 我认为您要使用的C#代码必须编译为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. 完成后,简单地将对该项目的引用添加到VB.Net项目,导入所需的命名空间,然后就可以使用C#代码。

Also see How To: Create and Use C# DLLs (from MSDN, for VS2005) 另请参见如何:创建和使用C#DLL (来自MSDN,适用于VS2005)

You also want to ensure that you C# code is CLS compliant. 您还希望确保C#代码符合CLS。 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). 这意味着它不会公开暴露其他.NET语言无法理解的任何功能(例如,无符号的int - VB中不存在,或者只是大小写不同的类 - 因为VB不区分大小写)。 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 article shows you how to do this: 本文介绍如何执行此操作:

The CLSCompliantAttribute can be applied to assemblies, modules, types, and members. CLSCompliantAttribute可以应用于程序集,模块,类型和成员。

For marking an entire assembly as CLS compliant the following syntax is used 要将整个程序集标记为符合CLS,请使用以下语法

 using System; [assembly:CLSCompliant(true)] 

For marking a particular method as CLS compliant the following syntax is used 为了将特定方法标记为符合CLS,使用以下语法

 [CLSCompliant(true)] public void MyMethod()` 

Put VB.NET and C# code in separate projects. 将VB.NET和C#代码放在不同的项目中。 (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/ ,效果很好)。

You don't need to worry about DLLs, just reference the project (use tab "Project" in the "Add Reference" dialog box). 您不必担心DLL,只需引用项目 (使用“添加引用”对话框中的“项目”选项卡)。 Eg if you need to use a function in the C# code/project add a reference in the VB.NET project to the C# project. 例如,如果您需要在C#代码/项目中使用函数,请在VB.NET项目中向C#项目添加引用。

You can't use a C# file and VB file in the same project. 您不能在同一项目中使用C#文件和VB文件。 You can, however, have VB and C# projects in the same solution and reference them. 但是,您可以在同一解决方案中使用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#项目,反之亦然。

You must also know that if you have a VB.NET project 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. 您还必须知道,如果您在同一解决方案中有一个带有C#项目的VB.NET项目,其中一个项目具有对另一个的引用,则在重建解决方案后,引用项目中的更改将仅供另一个使用。 It's like having binary reference, but with the capability to change code on the same solution. 它就像拥有二进制引用,但具有在同一解决方案上更改代码的能力。

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. 就个人而言,我不喜欢这样,但我总是处于这样的情况:我修改了引用项目中的代码,并且不知道为什么我的更改不在我使用它的代码中,我想出来了,哦,哦,我必须重建。

For temporary help, it could be acceptable but not for programming every day. 对于临时帮助,它可以接受,但不是每天编程。

If you were only planning on using the module in Visual Basic projects, then you should consider just converting the code to Visual Basic. 如果您只打算在Visual Basic项目中使用该模块,那么您应该考虑将代码转换为Visual Basic。 If you need to use the module in both C# and VB.NET programs I would use one of the solutions posted above 如果您需要在C#和VB.NET程序中使用该模块,我将使用上面发布的解决方案之一

You might try something like * Convert C# to VB.NET . 您可以尝试* 将C#转换为VB.NET It converts C# to VB.NET code. 它将C#转换为VB.NET代码。 I use this page exclusively when I have to convert something I find on the net that was written in C#. 当我必须转换我在网上用C#编写的东西时,我专门使用这个页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM