Analyzers and source generators
On this page
Current analyzer
TUtility.Analyzer runs PreferUtcNowAnalyzer at compile time:
| Diagnostic | Default level | Check |
|---|---|---|
CT0002 | Warning | Suggests DateTime.UtcNow for System.DateTime.Now member access |
The rule identifies System.DateTime.Now through semantic symbols and skips generated code.
Severity configuration
Configure your project’s .editorconfig:
# 持久时间默认使用 UTC,编译时保留明确提醒。
[*.cs]
dotnet_diagnostic.CT0002.severity = warningAnalyzers are build dependencies with no runtime invocation. If diagnostics are missing, inspect package references, analyzer assets and build logs.
Startup generation
TUtility.Generator contains AppStartupGenerator. It discovers eligible AppStartup classes and emits direct calls in descending order through AppStartupRunner, removing runtime assembly scanning for this discovery path.
The host still calls the runner. Omitting registration or configuration leaves modules unregistered or uninitialized. Referenced startup classes require an order attribute; see hosting.
Build troubleshooting
Analyzer and generator projects target netstandard2.0 for compiler loading. This does not define the runtime libraries’ framework support. Use a compatible SDK and let the compiler own generated output.
For duplicate types or methods, check for a copied hand-maintained runner or generated files compiled twice. For missing modules, inspect references, accessibility, abstract classes and order attributes. JSON contexts use System.Text.Json’s generator separately; see JSON and AOT.