代码检查:创建托管类型不受支持。
该检查会报告在由 Burst 编译器编译的代码中创建托管类型的 new 表达式。
Burst 针对高性能的受限 C# 子集,仅支持非托管且兼容 Burst 的数据。 在 Burst 编译的代码中创建如 object、 List<T> 、异常或其他引用类型的托管对象是不受支持的,因为这需要托管堆分配和垃圾回收。
示例
在此示例中, new List<int>() 表达式被用于 Burst 编译作业的 执行 方法中。 此不受支持并会被标记。
using System.Collections.Generic;
using Unity.Burst;
using Unity.Jobs;
[BurstCompile]
public struct ExampleJob : IJob
{
public void Execute()
{
// Reported: Creating managed types is not supported in Burst
var values = new List<int>();
}
}
using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
[BurstCompile]
public struct ExampleJob : IJob
{
public NativeArray<int> values;
public void Execute()
{
// Use Burst-compatible value types or native containers
var first = values[0];
}
}
快速修复
此检查不提供专用的快速修复。 请手动修复,移除托管分配并用兼容 Burst 的数据替换,例如实例字段、作业数据或受支持的原生容器。
2026年 5月 8日