C#编译器可以编译VB.Net代码吗?

编程入门 行业动态 更新时间:2024-10-15 14:18:22
本文介绍了C#编译器可以编译VB.Net代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在ASP.NET书上找到了以下内容。我正在学习和好奇关于以下内容。

IL架构的第二个主要优点是它使框架语言中立。在很大程度上,语言选择不再由语言的能力决定,而是由他们对开发者或tam的偏好决定。您甚至可以在单个应用程序中混合使用语言。用C#编写的类可以从VB2005类派生,在C#方法中抛出的异常可以在VB @ 005方法中捕获。

$ b $我的问题是,ASP.NET是否使用相同的编译器来编译VB和C#?

Update: )

可以C#编译器编译VB.Net代码吗?

解决方案

他们有单独的编译器(csc.exe为C#和VB.NET的vbc.exe),但他们都编译成IL,然后在运行时JIT将其编译成机器代码。

问题1:可以用C#编译器编译VB代码吗?

答案1:不,它不能,你会看到在下面的例子。如果你试图这样做,它会给出一个错误,因为它寻找C#语法。

问题2:我认为这是他们在书中说的。因为它是两个编译器,我觉得它是不兼容的

答案2:它不说你可以编译VB代码使用C#,但它说,你可以混合在单个应用程序中的语言,就像我在下面的例子中,仍然能够编译C#和VB(使用他们的编译器)。

请参阅下面的示例来了解它是如何工作的。我创建了一个解决方案与C#项目与C#类和VB项目与VB类。你不应该在同一个项目中混合使用C#和VB类,因为它会在编译过程中忽略vb文件。

ClassCSharp.cs的内容:

命名空间ClassLibraryCSharp { public abstract class ClassCSharp { public int MyProperty {get;组; } protected abstract void Test(); } }

C#ClassLibrary中ClassVBInCSharp.vb的内容。查看我如何继承C#类,并访问其属性并覆盖该方法。

命名空间ClassLibraryVB 类ClassVBInCSharp Inherits ClassCSharp Property Test2 As Integer 受保护的覆盖子测试() Test2 = MyBase.MyProperty End Sub End类结束命名空间

查看以下命令我运行:

vbc.exe /reference:\"ClassLibraryCSharp.dll/ target:library /out:\"ClassLibraryCSharpVbVersion.dllClassVBInCSharp.vb Microsoft R)Visual Basic编译器版本12.0.20806.33440 版权所有(c)Microsoft Corporation。版权所有。

查看上文VB编译器用于编译vb类。

csc.exe /reference:\"ClassLibraryCSharp.dll/ target:library /out:\"ClassLibraryCSharpVersion.dllClassVBInCSharp.vb Microsoft Visual C#编译器版本4.0.30319.33440 for Microsoft(R).NET Framework 4.5 版权所有(C)Microsoft Corporation。版权所有。 ClassLibrary1 \ClassVBInCSharp.vb(1,1):error CS 0116:命名空间不能直接包含字段或方法等成员

如果我试图使用C#Compiler来编译vb类,它会在查找C#语法时抛出一个错误。

I found following on a ASP.NET book. I am learning and curious about following content.

The second major advantage of an IL architecture is that it enables the Framework to be language neutral. To a large degree, language choice is no longer dictated by the capabilities of none language over another but rather by their preferences of the developer or the tam. You can even mix language in a single applications. A class written in C# can be derived from a VB2005 class, and an exception thrown in a C# method van be caught in a VB@005 method.

My question is , does ASP.NET use same compiler to compile VB and C#?

Update: (Another query)

Can C# compiler compile a VB.Net code ?

解决方案

They have separate compilers (csc.exe for C# and vbc.exe for VB.Net) but they both get compiled into IL and then at run-time JIT compiles it into machine code.

Question 1 : can C# compiler compile VB code?

Answer 1: No it can't and you will see that in the below example. It gives an error if you try to do that as it looks for C# syntax.

Question 2: I think that's what they say in the book. Since it is two compilers, I feel it is not compatible

Answer 2: It doesn't say that you can compile VB code using C# but it says that you can mix languages in a single application like I did in the example below and still able to compile C# and VB (using their compilers).

See below example to understand how it works. I created a solution with a C# project with a C# class and a VB project with a VB class. You should not mix C# and VB classes in same project as it will ignore the vb file if its a C# project during build.

Content of ClassCSharp.cs:

namespace ClassLibraryCSharp { public abstract class ClassCSharp { public int MyProperty { get; set; } protected abstract void Test(); } }

Content of ClassVBInCSharp.vb in C# ClassLibrary. See how I can inherit from a C# class and also access its properties and override the method.

Namespace ClassLibraryVB Public Class ClassVBInCSharp Inherits ClassCSharp Property Test2 As Integer Protected Overrides Sub Test() Test2 = MyBase.MyProperty End Sub End Class End Namespace

See below commands I ran:

vbc.exe /reference:"ClassLibraryCSharp.dll" /target:library /out:"ClassLibraryCSharpVbVersion.dll" "ClassVBInCSharp.vb" Microsoft (R) Visual Basic Compiler version 12.0.20806.33440 Copyright (c) Microsoft Corporation. All rights reserved.

See above VB Compiler is used to compile vb class.

csc.exe /reference:"ClassLibraryCSharp.dll" /target:library /out:"ClassLibraryCSharpVersion.dll" "ClassVBInCSharp.vb" Microsoft (R) Visual C# Compiler version 4.0.30319.33440 for Microsoft (R) .NET Framework 4.5 Copyright (C) Microsoft Corporation. All rights reserved. ClassLibrary1\ClassVBInCSharp.vb(1,1): error CS 0116: A namespace cannot directly contain members such as fields or methods

See above if I try to use C# Compiler to compile vb class it throws an error as its looking for C# syntax.

更多推荐

C#编译器可以编译VB.Net代码吗?

本文发布于:2023-11-13 22:29:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585500.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:编译器   代码   VB   Net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!