Note:
This is part of another article ". Net (C #): Talking about assembly list resources and resx resources.
All the classes mentioned are in the system. Resources namespace.
Let's look at the common members of these three classes:
Interface iresourcewriter: idisposable
Void addresource (string name, object/string/byte []);
// Add Resource Data
Void generate ();
// Update the buffer (flush)
Void close ();
// Call dispose
Interface iresourcereader: idisposable, ienumerable
Idictionaryenumerator getenumerator ();
// Obtain the dictionary iterator (used to enumerate resources)
Void close ();
// Call dispose
Class resourceset: idisposable, ienumerable
// No support for cultureinfo. encapsulate an iresourcereader
Constructor (Stream/iresourcereader/string );
Type getdefaultreader/writer ();
// Return the default iresourcereader/writer type
Idictionaryenumerator getenumerator ();
// Equivalent to iresourcereader. getenumerator
Object GetObject (string );
String getstring (string );
// With the bool parameter: Specifies whether it is case insensitive.
// Return the data with the specified name in the Resource
Two classes inherit from iresourcereader, namely resourcereader and resxresourcereader.
Iresourcewriter is resourcewriter and resxresourcewriter.
Resourcereader and resourcewriter operate on Binary resource files with the extension. Resources.
Resxresourcereader and resxresourcewriter operate on the resx file in XML format (note that the. Resources file is also used after resx compilation)
Constructors of these classes can specify strings to indicate the file path or stream object.
Finally, the resxresourcereader/writer type is in the system. Windows. Forms. dll class library. Resourcereader/writer is in the mscorlib. dll class library.
The following code uses iresourcewriter to write a resource file (in resourcewriter type, so it is a. Resources file), and then uses iresourcereader and resourceset to read the resource file:
// + Using system. Resources
Class Program
{
Static void main ()
{
// Use iresourcewriter
Write ();
// Use iresourcereader
Readusingresourcereader ();
// Use resourceset
Readusingresourceset ();
}
// Use iresourcewriter
Static void write ()
{
Using (iresourcewriter RW = new resourcewriter ("A. Resources "))
{
RW. addresource ("Byte", 0xff );
RW. addresource ("text", "Hellow ");
RW. addresource ("guid", guid. newguid ());
}
}
// Use iresourcereader
Static void readusingresourcereader ()
{
Console. writeline ("= Using iresourcereader ");
Using (iresourcereader RR = new resourcereader ("A. Resources "))
{
VaR iter = RR. getenumerator ();
While (ITER. movenext ())
Console. writeline ("key: {0} value: {1}", ITER. Key, ITER. value );
}
}
// Use resourceset
Static void readusingresourceset ()
{
Console. writeline ("= use resourceset ");
Using (resourceset rs = new resourceset ("A. Resources "))
{
Console. writeline (Rs. GetObject ("Byte "));
Console. writeline (Rs. getstring ("text "));
Console. writeline (Rs. GetObject ("guid "));
}
}
}
Output:
= Use iresourcereader
Key: Text Value: hellow
Key: guid value: 7f958dc9-eae2-43f5-a150-88fd8a07374e
Key: byte value: 255
= Use resourceset
255
Hellow
7f958dc9-eae2-43f5-a150-88fd8a07374e
. Net also provides a ResourceManager type to support multi-language resource extraction. The principle is through exploration. satellite assembly in. net. For the use of ResourceManager, refer to this article :. net (C #): Use the ResourceManager type.