The magic of indexers: reading configuration files

GPS platform, website construction, software development, system operation and maintenance, find Senda network technology!
http://cnsendnet.taobao.com
From Senda Technology Official Blog
http://www.cnsendblog.com/index.php/?p=602

Indexer: Officially, it is a member of a class that allows instances of a class or structure to be sorted in the same way as an array. An indexer is similar to an attribute except that the gei and set accessor methods of the indexer take parameters, while the property accessor methods do not.

Personal understanding: Indexer is a grammatical construct of C#, which allows you to access collections in classes in an array manner.An indexer is a special property that can specify its behavior as a property through get and set accessors, but with additional parameters to take it out of the set or assign it to it.

Example:

public class StringContainer
{
     private string[] strings;
     private int t = 0;

    public StringContainer(params string[] initialString)
      {
          strings = new string[200];
         foreach(string s in initialString)
          {
              strings[t++] = s;
          }
      }

     public void Add(string str)
     {
         if(t < strings.Length)
          {
              strings[t++] = str;
          }
      }

    //Start defining our indexer
      public string this[int index]
     {
         get
          {
             if(index < t)
              {
                 return strings[index];
              }
          }
         set
          {
             if(index < t)
              {
                  strings[index] = value;
              }
          }
      }

    public int Counts()

    {

          return this.t;

    }

}

Here we see the definition format of the indexer. He uses the keyword this to define the indexer. The syntax of the indexer is very similar to that of the attribute. Let's see how to call the defined indexer. His call is also very similar to the use of the attribute. Get information through get and set and append parameters or assign execution actions.

public class Test
{
      StringContainer scon = new StringContainer();
      scon.Add("a");
      scon.Add("b");
      scon.Add("c");
      scon.Add("e");

    string str = "abcd";
      scon[1] = str; // This side modifies [the second attribute value] to b as abcd by indexer

    for(int i=0; i<scon.Counts();i++)
     {
          Console.WriteLine("{0}:{1}", i , scon[i]);
      }
     Console.ReadLine();
 }

Take a look at the power of indexers: reading configuration files

public class SysConfig
     {
         // Fields
         private static SysRead m_SysRead;

         // Methods
         public static string sValue(string sKey)
          {
             string sPath = AppDomain.CurrentDomain.BaseDirectory + @"Config";
             if (!Directory.Exists(sPath))
              {
                  Directory.CreateDirectory(sPath);
              }
             string xmlFile = sPath + @"\Config.Xml";
             if (File.Exists(xmlFile))
              {
                  m_SysRead = new SysRead(xmlFile);
                 if (sKey == "Conn")
                  {
                     return m_SysRead.sConnection;
                  }
                 return m_SysRead[sKey];
              }
             //MessageBox.Show("Failed to read configuration file", "Prompt", "MessageBoxButtons.OK");
            return "";
          }
      }
    public class SysRead
     {
         // Fields
         private XmlDocument m_xmlDoc;
         private string m_xmlFile;
         private static XmlNode m_xmlNode;

         // Methods
         public SysRead(string sXmlFile)
          {
             try
              {
                 this.m_xmlFile = sXmlFile;
                 this.m_xmlDoc = new XmlDocument();
                 this.m_xmlDoc.Load(this.m_xmlFile);
                  m_xmlNode = this.m_xmlDoc.SelectSingleNode("Config");
              }
             catch
              {
                 //MessageBox.Show("Illegal characters in profile", "Tips", "MessageBoxButtons.OK");
            }
          }

         ~SysRead()//[Pu Yan Shu] Note that it's also a way to write with the ~symbol here, oh
         {
             m_xmlNode = null;
             this.m_xmlDoc = null;
          }

         private static string getConnValue(string sKey)
          {
             try
              {
                 string[] param = new string[2];
                  param = sKey.Split(new char[] { '.' });
                  XmlNodeList nodelist = m_xmlNode.ChildNodes;
                 foreach (XmlElement xE in nodelist)
                  {
                     if (xE.Name == param[0])
                      {
                         return xE.GetAttribute(param[1]);
                      }
                  }
              }
             catch
              {
                 return "";
              }
             return "";
          }

         // Properties
         public string this[string sKey]
          {
             get
              {
                 return getConnValue(sKey);
              }
          }

         public string sConnection
          {
             get
              {
                 return ("database=" + this["connect.DataBase"] + "; Server=" + this["connect.ServerIp"] + ";User ID=" + this["connect.Uid"] + ";Password=" + this["connect.Pw"] + ";Persist Security Info=True");
              }
          }
      }

GPS platform, website construction, software development, system operation and maintenance, find Senda network technology!
http://cnsendnet.taobao.com
From Senda Technology Official Blog
http://www.cnsendblog.com/index.php/?p=602

Tags: ASP.NET Attribute network PHP Database

Posted on Wed, 06 May 2020 00:05:45 -0400 by Mastermind