ISymbol.DeclaringSyntaxReferences 和 ISymbol.Locations 之间的区别

Difference between ISymbol.DeclaringSyntaxReferences and ISymbol.Locations

DeclaringSyntaxReferences property and Locations property in the ISyntax界面有什么区别?

答案线索在<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 也 returns 元数据引用声明和隐式声明的位置。您可以在 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。这是有道理的,因为您没有任何实际的源代码,因此 syntax 在编译中定义 System.Object.