代码检查:对按引用传递类的捕获字段引用可能导致运行时异常
从 MarshalByRefObject 派生的类型的字段通过引用传递时并不安全。 即使代码看起来有效,这样做在运行时也可能失败。 此检查会报告引用按引用传递类字段的 ref、 out 或 in 实参。
示例
using System;
class DataHolder : MarshalByRefObject
{
public int Value;
}
class Example
{
void Update(ref int x) { }
void Test(DataHolder holder)
{
Update(ref holder.Value);
}
}
using System;
class DataHolder : MarshalByRefObject
{
public int Value;
}
class Example
{
void Update(ref int x) { }
void Test(DataHolder holder)
{
var value = holder.Value;
Update(ref value);
}
}
2026年 5月 8日