When designing a program, whether it is the interface or background code, we usually want to leave users with a simpler interface. When I was involved in encapsulating the function package for voice card development, I found that the underlying function interfaces of various voice cards are all plastic variable tag values, which remain unchanged. As a result, I understood the original intention of using XML to build a reflection table mechanism in the code written by my predecessors. Next I will take a speech format compatible with a voice card as an example to illustrate the principle of the reflection table mechanism.
In the underlying development functions of voice cards, the parameters of Sound Play and recording functions are many and difficult to understand and remember. Take the voice format as an example. Assume that the mark value of the rate is 6, and the mark value of the u rate is 7 ,......, Generally, there are more than 10 compatible speech formats. If we use an integer variable to mark the voice format, we have two options: 1. or we can write down this relationship. either we can check the table when calling the function, and both methods require unnecessary investment.
At this time, the advantages of the reflection table are shown. Let's take a look at the example below:
// Do an enumeration of the voice format for your convenience:
Enum voice format
{
Undefined =-1,
A rate = 0,
U rate = 1,
PCM16 = 2,
PCM8 = 3,
GSM = 4,
ADPCM = 5,
VOX = 6,
MP3 = 7,
G729 = 8,
G723 = 9,
GC8 = 10,
}
// This is a speech format class used to fill the speech format table
Class PhoneticMatrix
{
XmlNodeList nodeList = null;
XmlNode node = null;
// Fill in the speech format table
Public readonly Dictionary <speech format, int> speech format table = new Dictionary <speech format, int> ();
Public void FillDirectory (string configuration file name)
{
XmlDocument xDoc = new XmlDocument ();
XDoc. Load (configuration file name );
// Select the format "Node" list
NodeList = xDoc. SelectNodes ("/speech format/format ");
Foreach (XmlNode nodes in nodeList)
{
// Select the node name and read the node Value
Node = nodes. SelectSingleNode ("name ");
String s name = node. FirstChild. Value;
// Select the node number and read the node Value
Node = nodes. SelectSingleNode ("no ");
String s number = node. FirstChild. Value;
Int I No. = int. Parse (s no );
// Fill in the speech format table
Speech format: Current speech format = (speech format) Enum. Parse (typeof (speech format), s name );
Speech format table. Add (current speech format, I number );
}
}
// Compile a method to demonstrate the usage of the voice format table. The method is similar but depends on the situation.
// Display the configuration number of the selected voice format
Public int ShowFormatID (voice format user option)
{
Int I voice format number;
// Query the configuration values corresponding to the selected enumerated items
Speech format table. TryGetValue (user option, out I speech format number );
Console. writeLine ("User selected items:" + User options. toString () + "; the configuration value corresponding to this option is:" + I voice format number. toString ());
Return I voice format number;
}
// Finally paste the Main function used for testing
Class Program
{
Static void Main (string [] args)
{
// Obtain the current working directory
String dir = Directory. GetCurrentDirectory ();
// Generate the file path
String actualDir = dir + "\ speech format. xml ";
PhoneticMatrix pm = new PhoneticMatrix ();
Pm. FillDirectory (actualDir );
Int I Format number = pm. ShowFormatID (speech format. a rate );
Console. ReadLine ();
}
}
// Finally, the XML file used for testing is provided:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Voice format>
<Format>
<Name> a rate </Name>
<No.> 6 </No.>
</Format>
<Format>
<Name> u rate </Name>
<No.> 7 </No.>
</Format>
<Format>
<Name> GSM </Name>
<No.> 49 </No.>
</Format>
<Format>
<Name> ADPCM </Name>
<No.> 17 </No.>
</Format>
<Format>
<Name> VOX </Name>
<No.> 23 </No.>
</Format>
<Format>
<Name> MP3 </Name