在 ASP.Net 网站项目中混合 VB.Net 和 C# 代码?

发布于 2024-08-23 05:16:30 字数 432 浏览 5 评论 0原文

这个问题相当古老并且经常被问到,我在这里也有类似的问题,但我的问题更具体一些。

Q1.在 ASP.Net 网站中混合使用 C# 和 VB.Net 代码是否合法?它会起作用吗?如果可行的话,该如何做?任何样品都会很好。

Q2。如果混合 C# 和 VB.Net 代码有任何影响,请也分享这些影响。

我有一个用 VB.Net 编码的 Web 项目。我正在研究该项目的一个模块。我想用 C# 编写代码。我无法将整个项目转换为 C#,因为我不是唯一一个从事该项目的人。然而,我打算构建的模块,我想用 C# 构建。

我听说在 Web 项目中,如果我们部分用 C# 编写代码,部分用 VB.net 编写,那么将项目编译为 dll 时就会出现问题。这是真的吗?如果是,那么解决方案是什么。

另外,如果我在 .Net 中构建动态链接库,那么我可以混合 C# 和 Vb.Net 代码吗?

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.

Q2. If there are any repercussions of mixing C# and VB.Net code then please do share those as well.

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#.

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.

Also, if i am building a dynamic link library in .Net then can i mix C# and Vb.Net code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

惯饮孤独 2024-08-30 05:16:30

来自http://msdn.microsoft.com/en-us/library/t990ks23。 aspx

App_Code 文件夹中的多种编程语言

由于 App_Code 文件夹中的源代码被编译为单个程序集,因此 App_Code 文件夹中的所有文件必须使用相同的编程语言。例如,App_Code 文件夹不能同时包含 Visual Basic 和 C# 中的源代码。

但是,您可以将 Web 应用程序配置为将 App_Code 文件夹的子文件夹视为单独的可编译单元。每个文件夹都可以包含不同编程语言的源代码。通过在 Web.config 文件的编译元素中创建 codeSubDirectories 元素并添加对子文件夹的引用来指定配置。以下示例说明如何配置名为 VBCode 和 CSCode 的子文件夹以编译为单独的程序集:

<compilation debug="false">
    <codeSubDirectories>
        <add directoryName="VBCode" />
        <add directoryName="CSCode" />
    </codeSubDirectories>
</compilation>

对 VBCode 和 CSCode 子文件夹的引用不需要包含有关子文件夹中包含的编程语言的任何信息。与 App_Code 文件夹本身一样,ASP.NET 根据子文件夹中的文件推断要使用的编译器。

From http://msdn.microsoft.com/en-us/library/t990ks23.aspx:

Multiple Programming Languages in the App_Code Folder

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#.

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:

<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.

相思碎 2024-08-30 05:16:30

这里得票最多的答案仅适用于网站项目,不适用于 Web 应用程序项目。我认为很多开发人员普遍使用“Web 应用程序”这个术语,而忽略了这样一个事实:从技术上讲,它在 .NET 中是不同的东西。微软表示,“默认情况下,Web应用程序是根据项目文件中的语言设置进行编译的。可以例外,但相对困难。”因此,我假设做到这一点的唯一方法是在一个解决方案中拥有两个单独的项目。 (同样,网站项目没有解决方案或项目文件。)我不确定所有细节。

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.

路弥 2024-08-30 05:16:30

看来您可以将它们混合在同一个项目中,只要您将所需的代码片段作为控件即可。因此,如果主站点采用 C# 语言,并且您将所需的 VB 部分作为 ascx 控件进行操作,则可以将其拖放到站点中。

我没有意识到你可以做到这一点,但是一名团队成员在他正在做的一项工作中解决了这个问题。

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.

I did not realise that you could do that, but a team member worked it out on a piece of work he was doing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文