JetBrains Rider 2026.1 Help

代码检查:不支持访问托管索引器

该检查会检测在由 Unity 的 Burst 编译器编译的方法中尝试对托管对象(类)使用索引器(如 obj[index] )的行为。

Burst 专为高性能代码设计,只支持 C# 中可以“blittable”的子集,并避免托管对象和垃圾回收。 由于托管类存储在堆上并需要垃圾回收,因此在 Burst 编译的代码中无法安全地访问。

运作方式

分析器会监控所有在 Burst 编译环境中的元素访问表达式节点(如 myList[0]myDictionary["key"] 的代码),例如带有 [BurstCompile] 特性的作业。

它会将元素访问解析为其底层属性(即索引器)并检查包含类型。 如果包含类型是类(托管类型)且不是标准 C# 数组(在 Burst 中有特殊处理),则会触发警告。

示例

在此示例中, managedList[0] 访问被标记,是因为 List<T> 是托管类。 将其替换为 NativeArray<T> 可以解决此问题,因为它是 blittable 结构体。

using Unity.Burst; using Unity.Jobs; using System.Collections.Generic; [BurstCompile] public struct MyJob : IJob { public List<int> managedList; // Managed reference! public void Execute() { // Reported: Accessing a managed indexer from 'System.Collections.Generic.List<int>' is not supported int value = managedList[0]; } }
using Unity.Burst; using Unity.Jobs; using Unity.Collections; [BurstCompile] public struct MyJob : IJob { // Use Native containers instead of managed ones public NativeArray<int> nativeArray; public void Execute() { // Safe: NativeArray is a blittable struct and its indexer is supported by Burst int value = nativeArray[0]; } }
2026年 5月 8日