. Net (C #): Talking about assembly list resources and resx Resources

Source: Internet
Author: User

 

Directory

  • Assembly List Resources
  • Resx resource file
  • Use resourcereader and resourceset to parse binary resource files
  • Use ResourceManager to parse binary resource files
  • Look down at the designer. CS file of the resx resource file.

 

 

Returned directory

Assembly List Resources

What is the simplest way to embed resources in a set of programs? That is, use the "embedded resource" option in Visual Studio to create, which is equivalent to using the "/resource" parameter of CSC. The procedure is as follows: select a resource file in the Project of Visual Studio, select "attribute", and select embedded resource in build action in the property box. For example, set. file as a resource file.

 

After this A. File becomes an embedded resource, it will be saved to the Assembly List (assembly manifest) as a manifest Resource ):

 

The Assembly list is an indispensable element of the Assembly, from msdn, you can refer to more information about the assembly or assembly list here: http://msdn.microsoft.com/zh-cn/library/1w45z383 (V = vs.100 ). aspx, this article will not talk about it more.

 

Next, you should be concerned about how to use the resources in the assembly list.

We can use the getmanifestresourcenames method of the Assembly class to return the names of all list Resources in the Assembly.

Or the Assembly. getmenifestresourcestream method returns the stream of the specified resource.

 

For example, the Assembly that contains a. File.

VaR ass = assembly. getexecutingassembly ();

Foreach (var file in ass. getmanifestresourcenames ())

Console. writeline (File );

 

Output

Mgen. A. File

The preceding mgen is the default namespace of the Assembly. After compiling, vs automatically adds the namespace before the file name.

 

Next, use getmanifestresourcestream to read the file content:

VaR ass = assembly. getexecutingassembly ();

VaR stream = ass. getmanifestresourcestream ("mgen. A. File ");

/* Operate the Stream Object To Read File Information */

 

 

Returned directory

Resx resource file

Another form of resource creation is the resx resource file, which is the "resource file" type in the file added through. The biggest advantage of the resx file over the manual creation of the Assembly List resources described above is:

  • Supports multiple languages
  • Quick Resource Creation
  • Convenient Management

Resx supports multiple languages. After Visual Studio is compiled, the affiliated Assembly (satellite assembly)) appears, and the connector (al.exe) is used to perform the job. When a program is executed in different language Environments, resources in the corresponding language are searched. Visual Studio also provides a powerful resx resource editor.

 

Like assembly list resources, we still need to understand how the so-called resx resources are stored.

Now, create a resource1.resx in the project, edit it, add a B. File file, add a string, and write a name at will.

 

After that, you will find some files in the project:

 

A. file is the assembly list resource we manually added above. The files in resource1.resx are stored in a folder named resources (B. file in the figure ).

 

Check the build actions in the properties of these two files. You will find that the build actions of files in resources are none, and Vs will not perform any operations on them, it's like they're not in the project. The build action of the resx file is embedded resource, which will become a programmer list resource. Unlike different assembly list resources, resx's custom tool during compilation is a tool called resxfilecodegenerator:

 

This tool connects all resx resources to create a binary file. vs finally saves the generated file as the Assembly List resource file to the Assembly. The extension of the binary resource file is. Resources.

 

Run the Assembly. getmanifestresourcenames method mentioned above to enumerate the Assembly List resource file. The output will be:

Mgen. A. File

Mgen. resource1.resources

 

The resource1.resx file is finally compiled into the mgen. resource1.resources resource file.

You can see this figure throughout the process:

 

 

Returned directory

Use resourcereader and resourceset to parse binary resource files

We recommend that you read this article to learn about iresourcereader, iresourcewriter, and resourceset types:. Net (C #): iresourcereader, iresourcewriter, and resourceset. The usage of these three types is not described here.

 

As mentioned above, the resx resource file will eventually be compiled into a resource file with the. Resources extension (Binary) and saved in assembly manifest resource ).

Next we will use the. Resources binary resource file parsing class resourcereader and resourceset in. Net to manually parse this. Resources file.

 

Code:

// + Using system. Resources

Static void main ()

{

Using (Stream resources = assembly. getexecutingassembly (). getmanifestresourcestream ("mgen. resource1.resources "))

{

// Use iresourcereader

Readusingresourcereader (resources );

// Relocates stream.

Resources. Seek (0, seekorigin. Begin );

// Use resourceset

Readusingresourceset (resources );

}

}

 

// Use iresourcereader

Static void readusingresourcereader (Stream st)

{

Console. writeline ("= Using iresourcereader ");

Iresourcereader RR = new resourcereader (ST );

VaR iter = RR. getenumerator ();

While (ITER. movenext ())

Console. writeline ("key: {0} value: {1}", ITER. Key, ITER. value );

// You do not need to call iresourcereader. Dispose. Stream will be called by dipose in the main method

}

// Use resourceset

Static void readusingresourceset (Stream st)

{

Console. writeline ("= use resourceset ");

Resourceset rs = new resourceset (New resourcereader (ST ));

Console. writeline (bitconverter. tostring (byte []) Rs. GetObject ("B ")));

Console. writeline (Rs. getstring ("string1 "));

// Resourceset. Dispose does not need to be called. Stream will be called by dipose in the main method

}

This will output the byte content of B. file and the string1 string in two ways: resourcereader and resourceset.

 

 

Returned directory

Use ResourceManager to parse binary resource files

For how to use the ResourceManager type, see. Net (C #): Use the ResourceManager type. I will not talk more here.

We directly use ResourceManager, or the above project, and use ResourceManager to parse the. Resources binary resource file.

 

Code:

// + Using system. Resources

ResourceManager resmanager = new ResourceManager (typeof (resource1 ));

// Equivalent to: New ResourceManager ("mgen. resource1", assembly. getexecutingassembly ());

// At this time, ResourceManager. basename is type. fullname is exactly mgen. resource1

 

// Obtain the content of file. B

Console. writeline (bitconverter. tostring (byte []) resmanager. GetObject ("B ")));

// Obtain the string in the Resource

Console. writeline (resmanager. getstring ("string1 "));

This will output the byte content of B. file and the string1 string.

 

 

Returned directory

Look down at the designer. CS file of the resx resource file.

Finally, let's look at the XXX. Designer. CS file after the resx resource file.

 

It defines a class for reading resources. For example, the resource file name is resource1 and the class name is resource1. In fact, this class encapsulates a ResourceManager mentioned above and has a strong property value to read files according to the definition of resource data displayed by the user resx.

 

ResourceManager is initialized in this way. You can see that ResourceManager. basename is the name of the Assembly List Resource (note ResourceManager. the basename attribute does not have the cultureinfo name and. resources extension, but there is a namespace (in fact, it is the file name), so the mgen. resource1.resources assembly list resource file ResourceManager basename is initialized: mgen. resource1 .)

Internal static Global: system. Resources. ResourceManager {

Get {

If (object. referenceequals (resourceman, null )){

Global: system. Resources. ResourceManager temp = new global: system. Resources. ResourceManager ("mgen. resource1", typeof (resource1). Assembly );

Resourceman = temp;

}

Return resourceman;

}

}

 

The file B and the string string1 resources defined in resx are completely packaged by the ResourceManager method. For example, if file B reads the returned byte array, it calls ResourceManager. GetObject and converts it to byte []:

Internal static byte [] B {

Get {

Object OBJ = ResourceManager. GetObject ("B", resourceculture );

Return (byte []) (OBJ ));

}

}

 

Now, let's get to it. I hope you can have a better understanding of the resx file and assembly list resources after reading the article!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.