代码检查:共享 static 类型参数需要 unmanaged 约束
此检查会报告在 SharedStatic<T> 使用时, T 不是非托管类型的情况。
SharedStatic<T> 旨在用于非托管数据。 如果类型实参包含像 object、 string 这样的托管字段、类引用或其他托管成员,则在调用 GetOrCreate 时会收到检查警告。
示例
在此示例中, SharedValue 结构体包含一个 object 字段,使其成为托管类型。 不能将其作为 SharedStatic<T> 的类型实参使用。
using Unity.Burst;
public struct SharedValue
{
public int count;
public object tag;
}
public class Example
{
private void Create()
{
// Reported: SharedValue is not unmanaged
var value = SharedStatic<SharedValue>.GetOrCreate<int>();
}
}
using Unity.Burst;
public struct SharedValue
{
public int count;
public double tag;
}
public class Example
{
private void Create()
{
// Correct: SharedValue is fully unmanaged
var value = SharedStatic<SharedValue>.GetOrCreate<int>();
}
}
快速修复
此检查不提供专用快速修复。 请手动将 SharedStatic<T> 类型实参完全改为非托管类型来修复问题。
2026年 5月 8日