Skip to main content
Source: Al0102AvoidTypeGetTypeAnalyzer.cs

Description

Type.GetType() with a dynamically constructed type name prevents the AOT compiler from performing static analysis, potentially trimming required types.

Bad Code

var typeName = $"MyApp.{entityName}Repository";
var type = Type.GetType(typeName);

Good Code

// Use a dictionary or switch for known types
var type = entityName switch
{
    "Order" => typeof(OrderRepository),
    "Product" => typeof(ProductRepository),
    _ => throw new NotSupportedException()
};

Properties

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

Configuration

dotnet_diagnostic.AL0102.severity = warning