Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is .NET renaming my embedded resources?

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.

like image 782
Colonel Panic Avatar asked Feb 05 '13 10:02

Colonel Panic


People also ask

What is embedded resource C#?

Embedded files are called as Embedded Resources and these files can be accessed at runtime using the Assembly class of the System.Reflection namespace. This article will illustrate, how to read and display an embedded Text file and Image file in Windows Application (Windows Forms) in C# and VB.Net.

How do I add embedded resources to Visual Studio?

Open Solution Explorer add files you want to embed. Right click on the files then click on Properties . In Properties window and change Build Action to Embedded Resource . After that you should write the embedded resources to file in order to be able to run it.

What is a manifest resource?

A manifest resource is a resource (such as an image file) that is embedded in the assembly at compile time. For more information about manifest resources, see Microsoft .


1 Answers

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.

like image 155
Hans Passant Avatar answered Sep 18 '22 15:09

Hans Passant