Reputation: 137742
When I build a file as 'embedded resource', Visual Studio gives it a name in the assembly depending on its path in the project. Eg. my file at cases/2013.1/colours.xml
is given a resource name with sporadic underscores something like cases._2013._1.colours.xml
.
My question is - how does is this name determined? Are the rules documented? Where is the method that Visual Studio uses?
Edit: I ask because I'm working with a large number of these things and it would be helpful to be able to deduce the resource name from the file path.
Upvotes: 14
Views: 3888
Reputation: 29963
The simple rules that you can apply to your example are...
cases/2013.1/colours.xml
1) replace "/" with "."
cases.2013.1.colours.xml
2) for each item in the dot delimited string, if the item begins with a digit, prepend an underscore to it.
cases => cases
2013 => _2013
1 => _1
colours => colours
xml => xml
3) reassemble the string, delimited with a dot.
cases._2013._1.colours.xml
Upvotes: 0
Reputation: 942256
As documented by the MSDN Library article, Resgen.exe uses the rules implemented by the StronglyTypedResourceBuilder.VerifyResourceName() method. I'll just copy what the MSDN library says:
If the key parameter is an empty string (""), a string that consists of a single underscore character (_) is returned. If the key parameter is not an empty string, the VerifyResourceName method compares each character in the string to a set of invalid tokens based on the language specified by the provider parameter. Any invalid character in the string is replaced with an underscore character. The characters that will be replaced with an underscore are as follows:
' ' (space), U+00A0 (non-breaking space), '.' (period), ',' (comma), ';' (semicolon), '|', '~', '@', '#', '%', '^', '&', '*', '+', '-', '/', '\', '<', '>', '?', '[', ']', '(', ')', '{', '}', '"' (quote), ''' (apostrophe), ':', and '!'.
Note
Strongly-typed resources do not allow the use of language keywords (such as if, for, and so on) as resource key names. However, the System.CodeDom design pattern allows the use of language keywords by prefixing the keyword with the underscore character. The VerifyResourceName method calls the CreateValidIdentifier method to enforce this design. For example, if you use a resource name that is the same as a language keyword, such as for, the name appears as _for in the generated strongly-typed resource class.
Looking at the source code for the StronglyTypedResourceBuilder class, the documentation is accurate.
Upvotes: 19
Reputation: 15199
I'm not aware of any documentation of how Visual Studio itself generates the names, but I would typically assume that it's compatible with the algorithm used by resgen.exe for standalone builds, which is documented here. However, the algorithm described does not work for your example (unless I'm misinterpreting it, it would produce a class name of cases._013._.colours
), so either VS implements it incorrectly, or the resgen documentation is inaccurate for the specific case you describe.
Upvotes: 1