• 加载类成员技巧


     今天写了一个读取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);
        }

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

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

    多思考,多创新,才是正道!
  • 相关阅读:
    flex属性导图
    html代码换行造成空格间距问题
    iconfont作用在css伪类中的写法
    JS模态框 简单案例
    JS实时获取输入框中的值
    JS封装addClass、removeClass
    特效 左右滑动轮播图jQuery思路
    JS 字符串两边截取空白的trim()方法的封装
    JavaScript易混淆知识点小回顾--数组方法与字符串方法;
    用GitHub来展示前端页面
  • 原文地址:https://www.cnblogs.com/shuang121/p/1974933.html
Copyright © 2020-2023  润新知