c# - ISymbol.DeclaringSyntaxReferences 和 ISymbol.Locations 之间的区别

标签 c# roslyn roslyn-code-analysis

DeclaringSyntaxReferences 之间有什么区别?属性(property)和Locations ISyntax 中的属性(property)界面?

最佳答案

答案的线索在 <remarks> 中评论区:

The syntax node(s) that declared the symbol. If the symbol was declared in metadata or was implicitly declared, returns an empty read-only array.

这意味着 Locations还返回元数据引用声明和隐式声明的位置。您可以在 LocationsTests.cs 中看到这方面的证据文件:

var c = s.GetTypeMembers("C", 0).Single() as NamedTypeSymbol;
var obj = c.BaseType;
Assert.Equal("MetadataFile(CommonLanguageRuntimeLibrary)", obj.Locations[0].ToString());

哪里cclass C在:

namespace N.S{class C{int F; void M(int P}{}}

所以 objSystem.Object .这是有道理的,因为您没有任何实际的源代码,因此在编译中没有定义 System.Object语法 .

关于c# - ISymbol.DeclaringSyntaxReferences 和 ISymbol.Locations 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38775807/

相关文章:

c# - 如何参数化 SQL 表而不会受到 SQL 注入(inject)的影响

c# - Unity 为非泛型接口(interface)注册泛型类型

C# Roslyn 编译 - 在动态编译的代码中使用 Nuget 包 - 编译但在运行时抛出异常(无法加载文件或程序集)

C# 2022 IDE - 没有关于代码样式或变量命名的警告?

c# - 我可以在定义它的解决方案中使用 roslyn 分析器吗?

c# - ParallelEnumerable.Aggregate 用于多种方法

c# - 将参数传递给 json webservice

c# - 如何使用 .NET 编译器平台创建 const 声明

c# - Roslyn Analyzer 操作应如何处理异步代码?

typescript - 如何使用 VSIX 中的 GetSyntaxRoot 获取 Typescript 作为 FileCodeModel 或 (Roslyn) 文档