问题描述
我在ASP.NET书上找到了以下内容。我正在学习和好奇关于以下内容。
$ 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.
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代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!