官术网_书友最值得收藏!

How it works...

Symbol analyzers register one or more symbol action callbacks to analyze symbol kinds of interest. Note that, unlike the default implementation that registered a delegate method named AnalyzeSymbol, we registered a lambda callback.

We specified interest in analyzing all the top-level symbol kinds that can have an enclosing type, namely types, methods, fields, properties, and events in the RegisterSymbolAction invocation:

context.RegisterSymbolAction(symbolContext =>
{
...
},
SymbolKind.NamedType,
SymbolKind.Method,
SymbolKind.Field,
SymbolKind.Event,
SymbolKind.Property);

The analyzer driver ensures that the registered lambda is invoked for all symbols of the registered interest kinds in the compilation.

Analysis skips the immediate enclosing type, as C# compiler already reports error CS0542, if a member has the same name as its enclosing type.

// Skip the immediate containing type, CS0542 already covers this case.
var outerType = symbolContext.Symbol.ContainingType?.ContainingType;

Core analysis works by looping over the outer types and comparing the name of the symbol in a symbol analysis context with the relevant outer types, until it finds a match, in which case, it reports a diagnostic; if the outer type has no containing type, it doesn t report a diagnostic.

while (outerType != null)
{
// Check if the current outer type has the same name as the given member.
if (symbolName.Equals(outerType.Name))
{
// For all such symbols, report a diagnostic.
...
}

outerType = outerType.ContainingType;
}
It is recommended that symbol actions only analyze and report diagnostics about declarations, not the executable code within it. If you need to analyze executable code within a symbol, you should try to register other action kinds discussed later in this chapter.
主站蜘蛛池模板: 高陵县| 嘉兴市| 云霄县| 达拉特旗| 仪陇县| 浏阳市| 新民市| 即墨市| 定安县| 临桂县| 德州市| 赤峰市| 麟游县| 乃东县| 白玉县| 开平市| 福安市| 合肥市| 大田县| 建湖县| 浦北县| 临朐县| 济阳县| 平乡县| 米脂县| 定陶县| 兰坪| 马龙县| 嘉兴市| 易门县| 清河县| 台山市| 临泽县| 哈尔滨市| 思南县| 礼泉县| 讷河市| 黄山市| 普宁市| 贺兰县| 玉环县|