Skip to main content
Source: AL0052AotSafeCallsAotUnsafeAnalyzer.cs

Description

Code marked with [AotSafe] guarantees it will work correctly in AOT-compiled applications. Calling methods marked with [AotUnsafe] violates this guarantee because [AotUnsafe] code explicitly declares it requires JIT compilation. This analyzer complements AL0044 which checks for [RequiresDynamicCode] calls. While AL0044 catches BCL-annotated unsafe methods, this analyzer catches user-defined unsafe code marked with [AotUnsafe].

Bad Code

[AotSafe]
public class SafeProcessor
{
    public void Process(object data)
    {
        // AL0052: Calling AotUnsafe code from AotSafe context
        ReflectionHelper.ExtractProperties(data);
    }
}

[AotUnsafe("Uses reflection for duck-typing")]
public static class ReflectionHelper
{
    public static void ExtractProperties(object obj)
    {
        var type = obj.GetType();
        foreach (var prop in type.GetProperties())
        {
            Console.WriteLine($"{prop.Name}: {prop.GetValue(obj)}");
        }
    }
}

Good Code

[AotSafe]
public class SafeProcessor
{
    public void Process(IDataContainer data)
    {
        // Use strongly-typed interface instead of reflection
        foreach (var (key, value) in data.GetProperties())
        {
            Console.WriteLine($"{key}: {value}");
        }
    }
}

public interface IDataContainer
{
    IEnumerable<KeyValuePair<string, object?>> GetProperties();
}

Properties

  • Category: AOT Testing
  • Severity: Error
  • Enabled by default: True
  • Code fix available: False

Configuration

dotnet_diagnostic.AL0052.severity = error

See Also