ReSharper 2026.1 Help

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

此检查会检测在使用 Unity 的 Burst 编译器编译的方法中,尝试对托管对象(类)使用索引器(例如, obj[index] )的操作。

Burst 旨在实现高性能代码,仅支持可 "blittable" 的 C# 子集,并避免使用托管对象和垃圾回收。 由于托管类存储在堆上并需要垃圾回收,所以无法在 Burst 编译的代码中安全访问。

运作方式

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

它会将元素访问解析为其底层属性(索引器),并检查其所属类型。 如果所属类型是类(托管类型)且不是标准 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日