简体   繁体   English

在VB.NET项目中使用C#类

[英]Using a C# class in a VB.NET project

I have a C# class (a lot of lines). 我有一个C#类(很多行)。 I want to use this class in my VB.NET project. 我想用这个class在我VB.NET项目。 So, I tried to add the .cs file to my VB.NET project and then call it by: 因此,我尝试将.cs文件添加到我的VB.NET项目中,然后通过以下方式调用它:

MyclassName.VoidName()

It's not working. 没用

( 'MycalssName' is not declared. It may be inaccessible due to its protection level. ) 'MycalssName' is not declared. It may be inaccessible due to its protection level.

How can I use a C# class in my VB.NET project? 如何在VB.NET项目中使用C# class

您可以简单地将C#代码编译为程序集(DLL),然后从您的VB.NET项目中引用该程序集。

A single .NET assembly/project should contain code written in one supported language only. 单个.NET程序集/项目应仅包含以一种受支持的语言编写的代码。 Compilation of any .NET project will need the language specific MSBuild targets (which are included as part of the project files). 任何.NET项目的编译都需要特定于语言的MSBuild目标(作为项目文件的一部分包含在内)。 So, you will be unable to compile/use a C# language source file in a VB.net project and vice versa. 因此,您将无法在VB.net项目中编译/使用C#语言源文件,反之亦然。

Ignoring the MSBUild aspects, the compilation of a vb.net project (with ac# source file) will not even suceed, as the syntax will be different and compiler (vbc.exe) will throw a lot of compiler errors. 忽略MSBUild方面,甚至不会成功编译vb.net项目(带有ac#源文件),因为语法会有所不同,并且编译器(vbc.exe)会引发很多编译器错误。

So, its best to compile the C# project and refer to the types from the VB.net project. 因此,最好编译C#项目并引用VB.net项目中的类型。

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

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