首页 > 解决方案 > 尝试构建时出现 CS1003 和 CS1026 语法错误

问题描述

我不确定为什么会出现此错误。我正在尝试在 Visual Studio 上构建它。我是新手,但我需要构建此代码。

有我的错误和代码:

(30,19): 错误 CS1003: 语法错误, '(' 预期

(30,88): 错误 CS1026:) 预期

(31,19): 错误 CS1003: 语法错误, '(' 预期

(31,51): 错误 CS1026:) 预期

(36,23): 错误 CS1003: 语法错误, '(' 预期

(36,63): 错误 CS1026:) 预期

(37,23): 错误 CS1003: 语法错误, '(' 预期

(37,156): 错误 CS1026:) 预期

namespace MelonLoader.AssemblyGenerator
{
    public static class DownloaderAndUnpacker
    {
        public static void Run(string url, string targetVersion, string currentVersion, string destinationFolder, string tempFile)
        {
            if (targetVersion == currentVersion)
            {
                Logger.Log($"{destinationFolder} already contains required version, skipping download");
                return;
            }
            
            Logger.Log($"Cleaning {destinationFolder}");
            foreach (var entry in Directory.EnumerateFileSystemEntries(destinationFolder))
            {
                if (Directory.Exists(entry))
                    Directory.Delete(entry, true);
                else
                    File.Delete(entry);
            }

            Logger.Log($"Downloading {url} to {tempFile}");
            Program.webClient.DownloadFile(url, tempFile);
            Logger.Log($"Extracting {tempFile} to {destinationFolder}");
            
/*line 30*/ using var stream = new FileStream(tempFile, FileMode.Open, FileAccess.Read);
            using var zip = new ZipArchive(stream);
            
            foreach (var zipArchiveEntry in zip.Entries)
            {
                Logger.Log($"Extracting {zipArchiveEntry.FullName}");
                using var entryStream = zipArchiveEntry.Open();
                using var targetStream = new FileStream(Path.Combine(destinationFolder, zipArchiveEntry.FullName), FileMode.OpenOrCreate, FileAccess.Write);
                entryStream.CopyTo(targetStream);
            }
        }
    }
}

标签: c#buildsyntax-error

解决方案


这似乎是 C# 8 “使用声明”;您可能有可用的最新编译器,但您的 csproj 配置为使用低级 C#;可以编辑cspoj以更新或添加:

<LangVersion>8</LangVersion>

(或更高,在一个<PropertyGroup>元素内)

会修复它。

如果你不能使用 C# 8,那么:

using (var stream = new FileStream(tempFile, FileMode.Open, FileAccess.Read))
using (var zip = new ZipArchive(stream))
{

    foreach (var zipArchiveEntry in zip.Entries)
    {
        Logger.Log($"Extracting {zipArchiveEntry.FullName}");
        using (var entryStream = zipArchiveEntry.Open())
        using (var targetStream = new FileStream(Path.Combine(destinationFolder, zipArchiveEntry.FullName), FileMode.OpenOrCreate, FileAccess.Write))
        {
            entryStream.CopyTo(targetStream);
        }
    }
}

推荐阅读