Do not pass literals as localized parameters

扶醉桌前 提交于 2020-05-15 02:39:26

问题


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

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.

This is my method:

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.

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?

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

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


回答1:


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


来源:https://stackoverflow.com/questions/40526908/do-not-pass-literals-as-localized-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!