代码检查:可能误导性的 'DefaultValueAttribute' 用法用于定义可选参数值
此检查报告使用 DefaultValueAttribute 来定义可选参数的默认值。 DefaultValueAttribute 不是可选参数元数据的正确属性。
示例
using System.Runtime.InteropServices;
using System.ComponentModel;
void Log([Optional, DefaultValue(1)] int level)
{
}
这看起来像是一个可选参数声明,但 DefaultValueAttribute 用于其它场景,在这里可能具有误导性。
如何修复它
没有针对此检查的专用快速修复。 对于可选参数,请使用 DefaultParameterValueAttribute。
using System.Runtime.InteropServices;
void Log([Optional, DefaultParameterValue(1)] int level)
{
}
2026年 5月 8日