Ish*_*eel 3 c# static-analysis casting compiler-warnings
有没有一种方法来生成隐编译时警告int,以long转换?(答案涉及静态分析工具,如FxCop会很好.)
int将a 转换为a long显然是一种安全的操作,但是我们说我们有一个库,它曾经拥有int其标识符的值,现在已升级为使用long所有这些值的值.
现在,客户端代码需要相应更新.因为如果客户端正在Int32为期望的方法提供参数Int64- 很可能需要更新客户端代码.
示例场景如下:
private void OnProcessGizmoClick()
{
    int gizmoId = 2;
    // I want the following usage to generate warnings:
    GizmoFactoryInstance.ProcessGizmo(gizmoId);
}
// Library code
public void ProcessGizmo(long gizmoId);
我认为最好的方法是使用Int32参数输入重载该方法,该输入在内部只能执行强制转换为Int64 - 但您的重载方法可能被标记为已弃用.
[Obsolete("Please use an Int64 for your identifier instead")]
然后Visual Studio将看到两个版本,并使用Int32声明,该声明将给出不推荐的警告.
如果在以后的版本中,或者对于您绝对不希望使用Int32参数调用的某些方法,您决定是否要导致编译器错误,您还可以将其更新为以下内容.
[Obsolete("Please use an Int64 for your identifier instead", true)]