问题
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