При использовании CSharpCompilation диагностические сообщения сообщают о неразрывных пробелах в файле как об ошибке.C#

Место общения программистов C#
Ответить
Anonymous
 При использовании CSharpCompilation диагностические сообщения сообщают о неразрывных пробелах в файле как об ошибке.

Сообщение Anonymous »

У меня есть исходные файлы с неразрывными пробелами. Иногда это просто 0xA0, но иногда это 0xC2 0xA0 (как пара).
Когда я анализирую эти файлы и передаю их CSharpCompilation, он возвращает диагностические сообщения сумки, подобные этому :

Код: Выделить всё

c:\xyz\SomeFile.cs(8,1): error CS1056: Unexpected character '�'
Вот как я компилирую код:

Код: Выделить всё

private static readonly List s_specificDiagnosticOptions = new[]
{
// Assembly 'AssemblyName1' uses 'TypeName' which has a higher version than referenced assembly 'AssemblyName2'
// https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/CS1705
"CS1705",
// Assuming assembly reference "Assembly Name #1" matches "Assembly Name #2", you may need to supply runtime policy
// https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/CS1701
"CS1701",
// Assuming assembly reference "Assembly Name #1" used by "Type Name #1" matches identity "Assembly Name #2" of "Type Name #2", you may need to supply runtime policy
"CS1702",
// 'member1' hides inherited member 'member2'. Use the new keyword if hiding was intended
// https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs0108
"CS0108",
// The member 'member' does not hide an inherited member. The new keyword is not required
// https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs0109
"CS0109",
// 'function1' hides inherited member 'function2'. To make the current method override that implementation, add the override keyword. Otherwise add the new keyword.
// https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs0114
"CS0114",
// The result of the expression is always 'value1' since a value of type 'value2' is never equal to 'null' of type 'value3'
// https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs0472
"CS0472",
// 'class' overrides Object.Equals(object o) but does not override Object.GetHashCode()
// https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs0659
"CS0659",
// Unreachable code detected
// https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs0162
"CS0162",
// Invalid name for a preprocessing symbol; '' is not a valid identifier
"CS8301",
// The annotation for nullable reference types should only be used in code within a '#nullable' annotations context
"CS8632",
// The using directive for 'XYZ' appeared previously as global using
"CS8933",
// Unnecessary using directive
"CS8019",
// 'member' is obsolete
// https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs0612
"CS0612",
// 'member' is obsolete: 'text'
// https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/CS0618
"CS0618"
}.Select(id => new KeyValuePair(id, ReportDiagnostic.Suppress)).ToList();
...
public static SyntaxTree ParseBytes(this byte[] bytes, CSharpParseOptions options, string filePath) => CSharpSyntaxTree.ParseText(SourceText.From(bytes, bytes.Length), options, filePath);
...
var compilation = CSharpCompilation.Create(asmProps.AssemblyName,
csFiles.Select(o => o.Bytes.ParseBytes(parseOptions, o.FilePath)),
references,
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary,
assemblyIdentityComparer: DesktopAssemblyIdentityComparer.Default,
generalDiagnosticOption: ReportDiagnostic.Error,
specificDiagnosticOptions: s_specificDiagnosticOptions));
Исходные файлы прекрасно компилируются компилятором C# в командной строке, поэтому я знаю, что использование неразрывных пробелов не должно быть проблемой.
Объект CSharpParseOptions просто содержит константы определения и указывает последнюю версию языка.
Как я могу проинструктировать CSharpCompiler не волноваться, увидев неразрывные пробелы? Я с осторожностью отношусь к простому подавлению CS1056, это кажется неправильным.

Подробнее здесь: https://stackoverflow.com/questions/793 ... ing-spaces
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»