C#编译器可以编译VB.Net代码吗? [英] Can C# compiler compile a VB.Net code?

查看:293
本文介绍了C#编译器可以编译VB.Net代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


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



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



Update:



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

解决方案

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



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



答案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.net 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.net 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代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆