构建 vSphere DLL 失败并出现 CS8078:表达式太长或太复杂,无法编译

Building vSphere DLLs fails with CS8078: An expression is too long or complex to compile

我正在关注此处的文档 Setting Up for Microsoft C# Development and at this step Building the C# vSphere DLLs 我在开发人员命令提示符中得到以下信息:

C:\Users\user\Downloads\VMware-vSphereSDK-6.5.0-4571253\SDK\vsphere-ws\dotnet\bin>build.bat
        1 file(s) copied.
Fixing HttpNfcLeaseInfo type, adding missing leaseState property
Generating VimService.cs
Microsoft (R) Service Model Metadata Tool
[Microsoft (R) Windows (R) Communication Foundation, Version 4.6.1055.0]
Copyright (c) Microsoft Corporation.  All rights reserved.

Generating files...
C:\Users\user\Downloads\VMware-vSphereSDK-6.5.0-4571253\SDK\vsphere-ws\dotnet\bin\VimService.cs
Compiling original VimService.dll
Microsoft (R) Service Model Metadata Tool
[Microsoft (R) Windows (R) Communication Foundation, Version 4.6.1055.0]
Copyright (c) Microsoft Corporation.  All rights reserved.

Generating XML serializers...
C:\Users\user\Downloads\VMware-vSphereSDK-6.5.0-4571253\SDK\vsphere-ws\dotnet\bin\VimServiceSerializers.cs
        1 file(s) copied.
Optimizing VimService.cs by stripping serializer hint attributes.
Compiling optimized VimService.dll
FAILED

查看 build.bat 似乎在这一行失败了:

echo Compiling optimized VimService.dll
csc /t:library /out:Vim25Service.dll VimService.cs VimServiceSerializers.cs >nul || goto ERROR

如果我 运行 csc /t:library /out:Vim25Service.dll VimService.cs VimServiceSerializers.cs 手动我得到以下内容:

C:\Users\user\Downloads\VMware-vSphereSDK-6.5.0-4571253\SDK\vsphere-ws\dotnet\bin>csc /t:library /out:Vim25Service.dll VimService.cs VimServiceSerializers.cs
Microsoft (R) Visual C# Compiler version 1.3.1.60616
Copyright (C) Microsoft Corporation. All rights reserved.

VimServiceSerializers.cs(32548,98): error CS8078: An expression is too long or complex to compile

我也试过 VS2017:

C:\Users\user\Downloads\VMware-vSphereSDK-6.5.0-4571253\SDK\vsphere-ws\dotnet\bin>csc /t:library /out:Vim25Service.dll VimService.cs VimServiceSerializers.cs
Microsoft (R) Visual C# Compiler version 2.0.0.61213
Copyright (C) Microsoft Corporation. All rights reserved.

VimServiceSerializers.cs(31372,109): error CS8078: An expression is too long or complex to compile

需要注意的一个行为,VimServiceSerializers.cs(#####,##) 上的行和列每次都不同。

谷歌搜索错误 CS8078,发现这是编译器 运行 堆栈溢出 space 的问题。

如何成功编译 VmWare 的代码?

我明白了。序列化程序 CS 文件有很长一段不间断的 if ... else if ... else if ... 子句。编译器必须立即处理整个 if/else 表达式,这导致它 运行 出栈 space。

幸运的是,这些 else if 中的每个分支都以 return 语句结束。这使得所有 else if 在功能上等同于独立的 if 语句,它们被独立解析。

在多处进行此替换后,文件即可编译。这是我修改后的 VimServiceSerializers.cs:https://1drv.ms/u/s!Al6mzY0CpY7EnHqBRDyg-z0ctrjk

通过将 if ...else 拆分为单独的 if 语句给出的答案是一种解决方案。另一个选项是检查用于编译代码的 C# 编译器的版本。我已经看到与 .NET 4.5、4.6 捆绑在一起的 csc.exe 可以编译此类代码而不会产生任何错误。但是 Roslyn .NET 编译器无法编译此类代码并生成 CS8078 错误。因此,如果您不想修改代码,另一个选择是更改 C# 编译器。例如下面的 csc.exe 可以编译这样的代码 -

 C:\Windows\Microsoft.NET\Framework\v4.0.30319>csc.exe /version
Microsoft (R) Visual C# Compiler version 4.6.1055.0
for Microsoft (R) .NET Framework 4.5
Copyright (C) Microsoft Corporation. All rights reserved.