Используя API Roslyn, я попытался получить пространство имен нулевого типа структуры (с использованием isymbol.contingNamespace) и был удивлен, когда он вернул пространство имен системы. Это происходит только для нулевых структур, оно работает так же, как и ожидалось для нулевых классов и не нулевых структур.
Exe
net9.0
13
enable
enable
< /code>
program.cs:
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace StructNamespaceRepro;
class Program
{
static void Main(string[] args)
{
string sourceText = """
// missing nullable context only causes an error for SomeClass? but not for SomeStruct?
#nullable enable
namespace StructNamespaceRepro.SomeNamespace {
internal class SomeClass;
internal struct SomeStruct;
internal class ClassContainingFieldDeclarations {
private SomeClass? _someClass = new();
private SomeStruct _someNonNullStruct = default;
private SomeStruct? _someNullableStruct = default;
}
}
""";
// compile sourcetext and create semantic model
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(sourceText);
CSharpCompilation compilation = CSharpCompilation.Create("AssemblyName", [syntaxTree],
[
// add reference to corelib
MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
]);
SemanticModel semanticModel = compilation.GetSemanticModel(syntaxTree);
if (semanticModel.GetDiagnostics().Length > 0)
{
throw new Exception("errors in source text");
}
// bind structdeclaration
StructDeclarationSyntax structDeclaration =
syntaxTree.GetRoot().DescendantNodes().OfType().First();
ITypeSymbol structType = semanticModel.GetDeclaredSymbol(structDeclaration);
// bind field declarations
List symbols = syntaxTree.GetRoot().DescendantNodes()
.OfType()
.Select(x => x.Declaration.Variables[0]).Select(x => semanticModel.GetDeclaredSymbol(x))
.Select(x => (IFieldSymbol)x!).ToList();
IFieldSymbol someClassField = symbols.Single(x => x.Name == "_someClass");
IFieldSymbol someNonNullStructField = symbols.Single(x => x.Name == "_someNonNullStruct");
IFieldSymbol someNullableStructField = symbols.Single(x => x.Name == "_someNullableStruct");
// accessing struct declaration works
Console.WriteLine(structType); // StructNamespaceRepro.SomeNamespace.SomeStruct
Console.WriteLine(structType.ContainingNamespace); // StructNamespaceRepro.SomeNamespace
// accessing nullable class field works
Console.WriteLine(someClassField.Type); // StructNamespaceRepro.SomeNamespace.SomeClass?
Console.WriteLine(someClassField.Type.ContainingNamespace); // StructNamespaceRepro.SomeNamespace
// accessing non-nullable struct field works
Console.WriteLine(someNonNullStructField.Type); // StructNamespaceRepro.SomeNamespace.SomeStruct
Console.WriteLine(someNonNullStructField.Type.ContainingNamespace); // StructNamespaceRepro.SomeNamespace
// accessing nullable struct field returns System namespace?
Console.WriteLine(someNullableStructField.Type); // StructNamespaceRepro.SomeNamespace.SomeStruct?
Console.WriteLine(someNullableStructField.Type.ContainingNamespace); // System
Подробнее здесь: https://stackoverflow.com/questions/797 ... le-structs
Почему isymbol.contingNamespace всегда возвращает систему для нулевых структур? ⇐ C#
Место общения программистов C#
1753647120
Anonymous
Используя API Roslyn, я попытался получить пространство имен нулевого типа структуры (с использованием isymbol.contingNamespace) и был удивлен, когда он вернул пространство имен системы. Это происходит только для нулевых структур, оно работает так же, как и ожидалось для нулевых классов и не нулевых структур.
Exe
net9.0
13
enable
enable
< /code>
program.cs:
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace StructNamespaceRepro;
class Program
{
static void Main(string[] args)
{
string sourceText = """
// missing nullable context only causes an error for SomeClass? but not for SomeStruct?
#nullable enable
namespace StructNamespaceRepro.SomeNamespace {
internal class SomeClass;
internal struct SomeStruct;
internal class ClassContainingFieldDeclarations {
private SomeClass? _someClass = new();
private SomeStruct _someNonNullStruct = default;
private SomeStruct? _someNullableStruct = default;
}
}
""";
// compile sourcetext and create semantic model
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(sourceText);
CSharpCompilation compilation = CSharpCompilation.Create("AssemblyName", [syntaxTree],
[
// add reference to corelib
MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
]);
SemanticModel semanticModel = compilation.GetSemanticModel(syntaxTree);
if (semanticModel.GetDiagnostics().Length > 0)
{
throw new Exception("errors in source text");
}
// bind structdeclaration
StructDeclarationSyntax structDeclaration =
syntaxTree.GetRoot().DescendantNodes().OfType().First();
ITypeSymbol structType = semanticModel.GetDeclaredSymbol(structDeclaration);
// bind field declarations
List symbols = syntaxTree.GetRoot().DescendantNodes()
.OfType()
.Select(x => x.Declaration.Variables[0]).Select(x => semanticModel.GetDeclaredSymbol(x))
.Select(x => (IFieldSymbol)x!).ToList();
IFieldSymbol someClassField = symbols.Single(x => x.Name == "_someClass");
IFieldSymbol someNonNullStructField = symbols.Single(x => x.Name == "_someNonNullStruct");
IFieldSymbol someNullableStructField = symbols.Single(x => x.Name == "_someNullableStruct");
// accessing struct declaration works
Console.WriteLine(structType); // StructNamespaceRepro.SomeNamespace.SomeStruct
Console.WriteLine(structType.ContainingNamespace); // StructNamespaceRepro.SomeNamespace
// accessing nullable class field works
Console.WriteLine(someClassField.Type); // StructNamespaceRepro.SomeNamespace.SomeClass?
Console.WriteLine(someClassField.Type.ContainingNamespace); // StructNamespaceRepro.SomeNamespace
// accessing non-nullable struct field works
Console.WriteLine(someNonNullStructField.Type); // StructNamespaceRepro.SomeNamespace.SomeStruct
Console.WriteLine(someNonNullStructField.Type.ContainingNamespace); // StructNamespaceRepro.SomeNamespace
// accessing nullable struct field returns System namespace?
Console.WriteLine(someNullableStructField.Type); // StructNamespaceRepro.SomeNamespace.SomeStruct?
Console.WriteLine(someNullableStructField.Type.ContainingNamespace); // System
Подробнее здесь: [url]https://stackoverflow.com/questions/79716649/why-does-isymbol-containingnamespace-always-return-system-for-nullable-structs[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия