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.
Q1。在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代码吗?
3 个解决方案
#1
49
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根据子文件夹中的文件推断出要使用的编译器。
#2
8
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应用程序是根据项目文件中的语言设置编译的。可以进行例外处理,但相对困难。”所以,我假设唯一的方法是在一个解决方案中有两个独立的项目。 (同样,网站项目没有解决方案或项目文件。)我不确定所有细节。
#3
0
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.
我没有意识到你可以做到这一点,但是一个团队成员正在研究他正在做的一项工作。
#1
49
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根据子文件夹中的文件推断出要使用的编译器。
#2
8
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应用程序是根据项目文件中的语言设置编译的。可以进行例外处理,但相对困难。”所以,我假设唯一的方法是在一个解决方案中有两个独立的项目。 (同样,网站项目没有解决方案或项目文件。)我不确定所有细节。
#3
0
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.
我没有意识到你可以做到这一点,但是一个团队成员正在研究他正在做的一项工作。