Skip to main content
Source: AL0053UnnecessaryAotUnsafeAnalyzer.cs

Description

This analyzer detects when [AotUnsafe] is applied to code that doesn’t actually use any AOT-incompatible patterns. This helps prevent over-annotation where developers mark code as unsafe “just to be safe” when it’s actually AOT-compatible. The analyzer checks for these AOT-incompatible patterns:
  • Calls to methods with [RequiresDynamicCode]
  • Calls to other [AotUnsafe] methods
  • Reflection APIs: Type.GetMethod, Type.GetProperty, PropertyInfo.GetValue, Activator.CreateInstance, etc.
  • Reflection.Emit namespace usage
  • dynamic keyword usage

Bad Code

Good Code

Properties

  • Category: AOT Testing
  • Severity: Warning (Suggestion)
  • Enabled by default: True
  • Code fix available: False

Configuration

Detected Patterns

The analyzer recognizes these as legitimate reasons for [AotUnsafe]:

See Also