zoukankan      html  css  js  c++  java
  • 加载类成员技巧

     今天写了一个读取Xml数据库的实例,其中在web.config中配置了xml的绝对路径,我们要读取这些路径

    <connectionStrings>
      <add name="BOARDFILEPATH" connectionString="~/XmlDatabase/Board.xml"/>
      <add name="TITLEFILEPATH" connectionString="~/XmlDatabase/Title.xml"/>
      <add name="REPLYFILEPATH" connectionString="~/XmlDatabase/Reply.xml"/>
      <add name="ATTACHMENTFILEPATH" connectionString="~/XmlDatabase/Attachment.xml"/>
      <add name="MESSAGEFILEPATH" connectionString="~/XmlDatabase/Message.xml"/>
      <add name="MESSAGESHIELDFILEPATH" connectionString="~/XmlDatabase/MessageShield.xml"/>
      <add name="USERSTATFILEPATH" connectionString="~/XmlDatabase/UserStat.xml"/>
     </connectionStrings>

    这些路径都是在项目中的,所以我们读到它的绝对路径,需要用到server.MapPath,在这里,我们创建了一个用来封装这些路径的类

    public class XmlBBS
     {

      public XmlBBS()
      {
       ///
      }

      private static string boardFilePath;
      public static string BoardFilePath
      {
       get { return boardFilePath; }
       set { boardFilePath = value; }
      }
      private static string titleFilePath;
      public static string TitleFilePath
      {
       get { return titleFilePath; }
       set { titleFilePath = value; }
      }
      private static string replyFilePath;
      public static string ReplyFilePath
      {
       get { return replyFilePath; }
       set { replyFilePath = value; }
      }
      private static string attachmentFilePath;
      public static string AttachmentFilePath
      {
       get { return attachmentFilePath; }
       set { attachmentFilePath = value; }
      }
      private static string messageFilePath;
      public static string MessageFilePath
      {
       get { return messageFilePath; }
       set { messageFilePath = value; }
      }
      private static string messageShieldFilePath;
      public static string MessageShieldFilePath
      {
       get { return messageShieldFilePath; }
       set { messageShieldFilePath = value; }
      }

      private static string userStatFilePath;
      public static string UserStatFilePath
      {
       get { return userStatFilePath; }
       set { userStatFilePath = value; }
      }

            public static void SystemInit(HttpServerUtility server)
            {
                boardFilePath = server.MapPath(
                    ConfigurationManager.ConnectionStrings["BOARDFILEPATH"].ConnectionString
                    );
                titleFilePath = server.MapPath(
                    ConfigurationManager.ConnectionStrings["TITLEFILEPATH"].ConnectionString
                    );
                replyFilePath = server.MapPath(
                    ConfigurationManager.ConnectionStrings["REPLYFILEPATH"].ConnectionString
                    );
                attachmentFilePath = server.MapPath(
                    ConfigurationManager.ConnectionStrings["ATTACHMENTFILEPATH"].ConnectionString
                    );
                messageFilePath = server.MapPath(
                    ConfigurationManager.ConnectionStrings["MESSAGEFILEPATH"].ConnectionString
                    );
                messageShieldFilePath = server.MapPath(
                    ConfigurationManager.ConnectionStrings["MESSAGESHIELDFILEPATH"].ConnectionString
                    );
                userStatFilePath = server.MapPath(
                    ConfigurationManager.ConnectionStrings["USERSTATFILEPATH"].ConnectionString
                    );
            }
     }

    在这里我们通过SystemInit方法读取了所有的路径,使用的时候直接调用这个方法,在这里,我们在全局应用程序中调用了这个方法,在程序启动的时候就执行

    void Application_Start(object sender, EventArgs e)
        {
          XmlBBS.SystemInit(Context.Server);
        }

    现在知道为什么上面类中的成员用的都是静态的了吧,只初始化一次,就可以在所有程序内使用,这就是使用静态的好处

    在这里我要说的就是全局应用程序和静态的使用技巧,有些复杂的东西,需要多次调用和加载的时候,我们可以考虑使用全局应用程序在启动的时候就加载一次,为了使其不消失,并在全局使用可以使用静态!

    多思考,多创新,才是正道!
  • 相关阅读:
    Non HTTP response code: org.apache.http.conn.HttpHostConnectException/Non HTTP response message
    elasticjobliteconsole的部署使用
    Loadrunner压测简易操作
    TestNg+Allure框架搭建
    bouncycastle类打包报错解决方法
    ie developer tools
    JS 三级联动 下拉列表
    HP大中华区总裁孙振耀退休感言
    spring如何配置和使用
    java 时间格式转换
  • 原文地址:https://www.cnblogs.com/shuang121/p/1974933.html
Copyright © 2011-2022 走看看