You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Mono中编译C#代码出现Unexpected symbol SpeechSynthesizer错误求解决

解决C#编译时Unexpected symbol SpeechSynthesizer错误

嘿,我来帮你搞定这个问题!你遇到的错误主要有两个核心原因,咱们一步步拆解解决:

1. 代码结构不符合C#的语法规范

C#是纯面向对象语言,所有可执行代码必须包裹在类中,而且需要一个Main方法作为程序的入口点。你的代码直接写了执行语句,没有类和入口方法的包裹,这会让编译器无法识别代码结构。

修正后的完整代码如下:

using System;
using System.Speech.Synthesis;

namespace SpeechDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            SpeechSynthesizer synth = new SpeechSynthesizer();
            synth.SetOutputToDefaultAudioDevice();
            synth.Speak("This example demonstrates a basic use of Speech Synthesizer");
            
            Console.WriteLine();
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
    }
}

2. 编译时需手动引用System.Speech.dll程序集

System.Speech.Synthesis命名空间所在的程序集不属于mcs编译器默认引用的基础程序集,所以你需要在编译命令中明确指定引用这个程序集。

正确的编译命令应该是:

mcs -r:System.Speech.dll -out:hello.exe hello.cs

这里的-r:参数用于告诉编译器要额外引用指定的程序集文件。

按照上面的代码和命令重新编译运行,应该就能正常执行语音合成功能啦!

内容的提问来源于stack exchange,提问作者mightguy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:21:40