Suppress "Resource name is not a valid identifier"

I have a project with 5000+ row resources. Almost all of them have periods in their identifier.

We switch to the automatic creation of strongly typed classes and, of course, due to periods, we see several thousand warnings

The resource name 'blah' is not a valid identifier.

I know that this is not so, the generator changes periods to underscores, and everything is in order.

Can I suppress a warning? It doesn't seem to have the associated C # pragma number.

+7
source share
4 answers

I think I can reproduce, although this is not a crystal in the matter. Project + Properties, Resources and enter the name of the string resource, for example "foo.bar", which triggers this warning.

Yes, you cannot suppress this warning; it is created by the resource developer in the IDE. When it automatically generates code from the Properties \ Resources.resx file to the Properties \ Resources.Designer.cs file. Look at the .resx file and check the "name" attribute on the <data> elements. A period in the attribute value will trigger a warning.

A carefully crafted regular expression search and replace can fix this by turning periods into underscores. This gives me personally two problems, I will just write a small program that uses XDocument. Also check if you need this automatically generated code, it looks like you are replacing it.

+6
source

This will suppress all warnings for this .cs file using

 #pragma warning disable 

You can also right-click on the warning, and then click on the Show Error help. It should indicate the exact warning number so that you can suppress only this warning for the entire project by going to the project properties, then on the build page and entering it in the Suppress warnings text box.

After further research, it appears that some warnings cannot be suppressed. See Details in msdn for compiler options / nowarn C # http://msdn.microsoft.com/en-us/library/7f28x9z3.aspx

One of them is the compiler warning (level 1) CS2029, which refers to the warning "invalid identifier". You can also confirm that it cannot be disabled by visiting the compiler warning (level1) CS2029 for details here http://msdn.microsoft.com/en-us/library/878y1894.aspx

+3
source

A REALLY terrible job here should be to undo the generated code from the Resource tool:

Say your resource.resx resource file name

  • Open the resource tool by double-clicking Resource.resx in a project inside Visual Studio.

  • Set the access modifier corresponding to the area of ​​the generated code that you are using.

  • In Windows Explorer, navigate to the directory where the Resource .resx and Resources.Designer.cs code is stored. (Either manually, or right-click on the project / namespace folder and select "Open Folder in Windows Explorer")

  • Rename Resources.Designer.cs to Resources.cs

  • Go back to the resource tool in Visual Studio and set the access modifier to "No code generation"

  • Save, close resource tool

  • Click the Show All Files icon at the top of the Solution Explorer window. The Resources.cs file should appear in the same folder as the .resx Resources in ghostly form

  • Right-click on Resources.cs and select "Include in Project"

Now the warnings should disappear. Of course, the inconvenience of this work is that you have to perform all these steps each time you change the resource file.

+2
source

This warning occurs if the resource name is not a valid identifier (for example, it contains a space or '-'), and strongly typed resource generation is turned on for the file. This is a warning instead of an error because the strongly typed resource name will be changed to a valid name (for example, "ab" will be changed to "a_b"), but the resource name will remain "a".

To fix this error

You must first change the resource name to a valid identifier, if possible. If this is not a valid option, turn off strongly typed resource code generation for this .resx file. To do this, select the .resx file in Solution Explorer and clear the Custom Tool property.

SOURCE: http://msdn.microsoft.com/en-us/library/ms228672(v=vs.80).aspx

0
source

All Articles