> ## Documentation Index
> Fetch the complete documentation index at: https://ancplua.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# AL0101 - Activator.CreateInstance is not AOT-safe

> Activator.CreateInstance uses reflection for instantiation

Source: [Al0101AvoidActivatorCreateInstanceAnalyzer.cs](https://github.com/ANcpLua/ANcpLua.Analyzers/blob/main/src/ANcpLua.Analyzers/Analyzers/Al0101AvoidActivatorCreateInstanceAnalyzer.cs)

## Description

`Activator.CreateInstance()` relies on reflection to create instances, which is not compatible with Native AOT trimming. Use direct construction or factory patterns.

## Bad Code

```csharp theme={null}
var service = Activator.CreateInstance(typeof(OrderService));
```

## Good Code

```csharp theme={null}
var service = new OrderService();
// Or use DI: serviceProvider.GetRequiredService<OrderService>()
```

## Properties

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

## Configuration

```editorconfig theme={null}
dotnet_diagnostic.AL0101.severity = warning
```
