代码检查:为避免分配,Lambda 表达式/匿名方法必须设为 'static'
此检查会报告在需要显式 static 的地方传递了无捕获 lambda表达式,例如传递给标记为 [RequireStaticDelegate] 的参数。
示例
void M([RequireStaticDelegate] Func<int, int> f) {}
void Test()
{
M(x => x + 1);
}
void M([RequireStaticDelegate] Func<int, int> f) {}
void Test()
{
M(static x => x + 1);
}
快速修复
为该 lambda表达式添加 static 修饰符。
2026年 5月 8日