JetBrains Rider 2026.1 Help

代码检查:可能的 'System.InvalidCastException'

此检查会报告在运行时可能因 InvalidCastException 而失败的显式转换。

示例

using System; static void Handle(object value) { if (value is Action<string>) { var action = (Action<int>)value; } }

类型检查证明 valueAction<string> ,因此将其转换为 Action<int> 并不安全。

如何修复

此检查没有专用的快速修复方案。 通常的修复方法是转换为实际检查过的类型,或使用模式匹配。

using System; static void Handle(object value) { if (value is Action<string> action) { action("text"); } }
2026年 5月 8日