- Roslyn Cookbook
- Manish Vasani
- 264字
- 2021-07-15 17:07:30
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;
}
- Delphi程序設計基礎:教程、實驗、習題
- Android和PHP開發(fā)最佳實踐(第2版)
- Boost C++ Application Development Cookbook(Second Edition)
- PyTorch自動駕駛視覺感知算法實戰(zhàn)
- 華為HMS生態(tài)與應用開發(fā)實戰(zhàn)
- Reactive Programming with Swift
- jQuery EasyUI網(wǎng)站開發(fā)實戰(zhàn)
- Animate CC二維動畫設計與制作(微課版)
- Java EE 7 Development with NetBeans 8
- Learning Selenium Testing Tools(Third Edition)
- MATLAB 2020從入門到精通
- Raspberry Pi Robotic Projects(Third Edition)
- ASP.NET Web API Security Essentials
- Learning Python Data Visualization
- 基于MATLAB的控制系統(tǒng)仿真及應用