在 ASP.Net 网站项目中混合使用 VB.Net 和 C# 代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2339776/
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 VB.Net and C# Code in an ASP.Net Web Site project?
提问by Steve Johnson
The question quite an older and often asked around, i have similar questions here but my question is a bit more specific.
这个问题很老而且经常被问到,我在这里也有类似的问题,但我的问题更具体一些。
Q1. Is it legal to mix C# and VB.Net code in ASP.Net Web site? Will it work or not? If it works how it may be done? Any sample will be good.
一季度。在 ASP.Net 网站中混合 C# 和 VB.Net 代码是否合法?它会起作用吗?如果它有效,怎么做?任何样品都会很好。
Q2. If there are any repercussions of mixing C# and VB.Net code then please do share those as well.
Q2。如果混合 C# 和 VB.Net 代码有任何影响,那么也请分享这些。
I have a web project that is coded in VB.Net. I am working on one module of the project. and i want to code in C#. I cant convert the whole project to C# because i am not the only one working on the project. However, the module i intend to build , i want to have that built in C#.
我有一个用 VB.Net 编码的 Web 项目。我正在研究该项目的一个模块。我想用 C# 编码。我无法将整个项目转换为 C#,因为我不是唯一一个在该项目上工作的人。但是,我打算构建的模块,我想用 C# 构建。
I have heard that in case of web projects, if we code part in C# and part in VB.net then there are problems compiling the project to a dll. Is that true? if yes then what is the solution.
我听说在 Web 项目的情况下,如果我们用 C# 编写代码,用 VB.net 编写代码,那么将项目编译为 dll 就会出现问题。真的吗?如果是,那么解决方案是什么。
Also, if i am building a dynamic link library in .Net then can i mix C# and Vb.Net code?
另外,如果我在 .Net 中构建动态链接库,那么我可以混合使用 C# 和 Vb.Net 代码吗?
采纳答案by Gabe
From http://msdn.microsoft.com/en-us/library/t990ks23.aspx:
从http://msdn.microsoft.com/en-us/library/t990ks23.aspx:
Multiple Programming Languages in the App_Code Folder
App_Code 文件夹中的多种编程语言
Because the source code in the App_Code folder is compiled into a single assembly, all the files in the App_Code folder must be in the same programming language. For example, the App_Code folder cannot include source code in both Visual Basic and C#.
由于 App_Code 文件夹中的源代码被编译成单个程序集,因此 App_Code 文件夹中的所有文件必须使用相同的编程语言。例如,App_Code 文件夹不能同时包含 Visual Basic 和 C# 中的源代码。
However, you can configure your Web application to treat subfolders of the App_Code folder as separate compilable units. Each folder can then contain source code in a different programming language. The configuration is specified by creating a codeSubDirectories element in the compilation element of the Web.config file and adding a reference to the subfolder. The following example illustrates how you would configure subfolders named VBCode and CSCode to compile into separate assemblies:
但是,您可以将 Web 应用程序配置为将 App_Code 文件夹的子文件夹视为单独的可编译单元。每个文件夹都可以包含不同编程语言的源代码。通过在 Web.config 文件的编译元素中创建 codeSubDirectories 元素并添加对子文件夹的引用来指定配置。以下示例说明了如何配置名为 VBCode 和 CSCode 的子文件夹以编译为单独的程序集:
<compilation debug="false">
<codeSubDirectories>
<add directoryName="VBCode" />
<add directoryName="CSCode" />
</codeSubDirectories>
</compilation>
The references to the VBCode and CSCode subfolders do not need to include any information about what programming language is contained in the subfolder. As with the App_Code folder itself, ASP.NET infers the compiler to use based on the files in the subfolder.
对 VBCode 和 CSCode 子文件夹的引用不需要包含有关子文件夹中包含什么编程语言的任何信息。与 App_Code 文件夹本身一样,ASP.NET 会根据子文件夹中的文件推断要使用的编译器。
回答by Leroy
It seems you can mix them within the same project is you do the piece of code you require as a control. So if the main site is in C# and you do the VB part you want as a ascx control, you can drap and drop that within your site.
似乎您可以在同一个项目中混合它们,因为您将需要的代码片段作为控件。因此,如果主站点使用 C# 并且您将 VB 部分作为 ascx 控件执行,您可以将其拖放到您的站点中。
I did not realise that you could do that, but a team member worked it out on a piece of work he was doing.
我没有意识到你可以做到这一点,但一名团队成员在他正在做的一项工作中解决了这个问题。
回答by user692918
The answer here that has the most votes only works for Web Site projects, not a Web App project. I think a lot of developers use the term "web application" generically ignoring the fact that technically it is a different thing in .NET. Microsoft says, "By default, a Web application is compiled based on language settings in the project file. Exceptions can be made, but it is relatively difficult." So, I am assuming the only way to do this would be to have two separate projects within one solution. (Again, a Web Site project has no solution or project file.) I'm not sure of all the details.
这里得票最多的答案仅适用于网站项目,而不适用于 Web App 项目。我认为很多开发人员使用术语“Web 应用程序”一般都忽略了一个事实,即从技术上讲它在 .NET 中是另一回事。微软表示,“默认情况下,Web 应用程序是根据项目文件中的语言设置编译的。可以有例外,但比较困难。” 因此,我假设唯一的方法是在一个解决方案中包含两个独立的项目。(同样,网站项目没有解决方案或项目文件。)我不确定所有细节。