代码检查:某些 SharedStatic`1.GetOrCreate 重载会导致编译器错误
此检查会报告在 SharedStatic<T>.GetOrCreate 重载中使用采用 System.Type 实参的方法,这些操作出现在由 Burst 编译器 编译的代码中。
这些重载在 C# 中可用,但在 Burst 中可能导致编译器错误。 检查建议改用通用 GetOrCreate<...>() 重载。
示例
在此示例中, GetOrCreate 方法以 typeof 表达式调用。 Burst 不支持此操作,并且会被标记出来。
using Unity.Burst;
using Unity.Jobs;
[BurstCompile]
public struct ExampleJob : IJob
{
public void Execute()
{
// Reported: typeof is not supported in GetOrCreate
var shared = SharedStatic<int>.GetOrCreate(typeof(double));
}
}
using Unity.Burst;
using Unity.Jobs;
[BurstCompile]
public struct ExampleJob : IJob
{
public void Execute()
{
// Correct: Use the generic overload
var shared = SharedStatic<int>.GetOrCreate<double>();
}
}
快速修复
此检查不提供专用快速修复。 请手动切换为通用 SharedStatic<T>.GetOrCreate 重载。 对于 2 个类型实参,使用 GetOrCreate<TContext, TSubContext>() ,而不是 GetOrCreate(typeof(...), typeof(...))。
2026年 5月 8日