CA1303,DoNotPassLiteralsAsLocalizedParameters,但实际上我不是 [英] CA1303, DoNotPassLiteralsAsLocalizedParameters, but I'm actually not

查看:66
本文介绍了CA1303,DoNotPassLiteralsAsLocalizedParameters,但实际上我不是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码从 Microsoft.Globalization 收到 CA1303 警告,请勿将文字作为本地化参数传递",但我的代码实际上没有传递文字:

private void MyForm_Load(object sender, EventArgs e)
{
    UpdateTitle();
}

private void UpdateTitle()
{
    Version version = Assembly.GetExecutingAssembly().GetName().Version;
    CultureInfo culture = CultureInfo.CurrentUICulture;
    this.Text = String.Format(culture, "{0} v{1}.{2} Alpha r{3}", this.Text, version.Major, version.Minor, version.Build);
}

此代码在每次加载表单时将表单的标题设置为以下形式:

This code sets the title of the form to something like this, every time it is loaded:

MyFormNameAsSetInTheDesigner v0.1 Alpha r123

MyFormNameAsSetInTheDesigner v0.1 Alpha r123

( version.build 实际上包含SVN修订版,该修订在每次提交时自动更新,我不使用 revision ,因为我的版本控制方案仅使用3个数字, major.minor.revision )

(version.build actually contains the SVN revision, which is auto-updated at each commit, I do not use revision because my versioning scheme uses only 3 numbers, major.minor.revision)

但这会触发上述警告,因为它认为我是根据字符串文字设置标题栏文本的.实际上,我已经在设计器中设置了 Localizable = True ,以便从资源表中获取字符串.

But this triggers the aforementioned warning, because it thinks I'm setting the titlebar text from a string literal. In fact, I've set Localizable = True in the designer, so that the string is fetched from a resource table.

我不想静态设置表单标题,因为(尤其是在alpha和beta阶段)我希望它具有动态版本号.

I do not want to set the form's title statically because (especially in the alpha and beta stages) I want it to have dynamic version numbers.

推荐答案

基于对于CA1303的文档,发出警告的原因是,您将文字字符串作为 String.Format 方法的第二个参数,以及该参数的第二个形式参数特殊的重载用 LocalizableAttribute 标注.

Based on the documentation for CA1303, the reason for the warning being raised is that you pass a literal string as the second parameter of the String.Format method, and in addition the second formal parameter of that particular overload is annotated with LocalizableAttribute.

因此,警告您要执行的操作是将字符串"{0} v {1}.{2} Alpha r {3}" 作为资源集中的本地化资源,并这样称呼它.这可能是一个好主意,因为从技术上讲,格式字符串的结构及其内容的固定部分是可本地化的资源.

Therefore what the warning wants you to do is to put the string "{0} v{1}.{2} Alpha r{3}" as a localized resource in your resource assembly, and refer to it as such. Which is probably a good idea, as technically the structure of the format string and the fixed parts of its contents are localizable resources.

如果只想关闭FxCop,则可以

If you simply want to make FxCop shut up, you can annotate UpdateTitle accordingly:

[SuppressMessage("Microsoft.Globalization",
                 "CA1303:DoNotPassLiteralsAsLocalizedParameters" )]
private void UpdateTitle() { /* ... */ }

这篇关于CA1303,DoNotPassLiteralsAsLocalizedParameters,但实际上我不是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆