不要将文字作为本地化参数传递 [英] Do not pass literals as localized parameters

查看:101
本文介绍了不要将文字作为本地化参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目(这是Windows Phone 8.1应用程序)上运行代码分析时,出现以下警告:

I have the following warning when running a code analysis on my project (which is a Windows Phone 8.1 app):

CA1303请勿将文字作为本地化参数传递方法'Common.TranslateError(String)'将文字字符串作为对'XDocument.Parse(String)'的调用的参数'text'传递.而是从资源表中检索以下字符串.

CA1303 Do not pass literals as localized parameters Method 'Common.TranslateError(String)' passes a literal string as parameter 'text' of a call to 'XDocument.Parse(String)'. Retrieve the following string(s) from a resource table instead.

这是我的方法:

Public Function TranslateError(ByVal exMessage As String) As XDocument

    Return XDocument.Parse("<Response><Exception><Message><" & XmlConvert.EncodeName(exMessage) & "></Message></Exception></Response>")

End Function

代码有效,自添加代码以来,我不必解决任何问题,但是此警告使我相信我做的事情不正确.

The code works and it's not something I've had to address since adding the code however this warning makes me believe I'm not doing something quite right.

我对此进行了一些研究,发现MSDN Acticle CA1303:不要将文字作为局部参数传递,但是我不能引用ResourceManager.如果我可以参考这一点,我仍然不明白为什么将字符串传递给XDocument.Parse时这是一个问题.

I've done some research into this and found the MSDN acticle CA1303: Do not pass literals as localized parameters however I can't reference to a ResourceManager. If I could reference to that I still wouldn't understand why this is a problem when passing a string to XDocument.Parse.

我想解决该警告,而不是予以抑制.有没有人知道如何解决此问题或为什么存在这样的警告?

I want to address the warning and not suppress it. Has anyone got any ideas how I can fix this or why such a warning exists?

如果要复制,则需要将规则集配置为使用 Microsoft所有规则:

Should you want to replicate you will need to configure the Rule Set to use Microsoft All Rules:

然后运行分析,从Visual Studio菜单中选择 ANALYZE ,然后选择在...上运行代码分析

Then to run the analysis select ANALYZE from the Visual Studio menu and choose Run Code Analysis on...

推荐答案

如@RyanRoos所建议,这段代码解决了警告:

As suggested by @RyanRoos this piece of code resolved the warning:

Public Function TranslateError(ByVal exMessage As String) As XDocument

    Dim sb As New StringBuilder("<Response><Exception><Message><![CDATA[" & XmlConvert.EncodeName(exMessage) & "]]></Message></Exception></Response>")

    Return XDocument.Parse(sb.ToString())

End Function

这篇关于不要将文字作为本地化参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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