如何获取CS-Script编译时的英文警告与错误信息
Got it, let's sort out how to switch those German error and warning messages to English when using CS-Script for runtime compilation. Here are two reliable approaches:
1. Set the VSLANG Environment Variable
The .NET compiler (csc.exe/Roslyn) uses the VSLANG environment variable to determine the language for output messages. Setting it to 1033 (the locale ID for English - US) will force English output.
You can set this either globally for your entire application or right before calling the CS-Script compilation:
// Set the environment variable before compiling Environment.SetEnvironmentVariable("VSLANG", "1033"); // Your existing compilation code var eval = CSScript.Evaluator; var assembly = eval.CompileCode(code); // Check for errors/warnings if (CSScript.CompilingHistory.Any()) { foreach (var err in CSScript.CompilingHistory.Last().Value.Result.Errors.Cast<CompilerError>()) { this.AddValidationResult(err, codeFile); } }
Note: If you need to revert back to the original language later, save the previous value of VSLANG and restore it after compilation.
2. Pass Compiler Options Directly to CS-Script
CS-Script lets you pass custom compiler flags to the underlying .NET compiler. You can specify the culture directly via compiler options:
var eval = CSScript.Evaluator; // Add the culture flag to force English output eval.CompilerOptions = "/culture:en-US"; var assembly = eval.CompileCode(code); // Your existing error checking code here...
This approach targets just the CS-Script evaluator instance, so it won't affect other parts of your application that might rely on the original locale.
Why This Works
CS-Script relies on the official .NET compiler (either csc.exe or Roslyn) under the hood. Both compilers respect the VSLANG environment variable and the /culture compiler option to determine the language of diagnostic messages. By setting either of these, you override the system's default locale for compiler output.
内容的提问来源于stack exchange,提问作者BennoDual

