跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • 求教 C#类的初始化变量问题
未分類
26 10 月 2020

求教 C#类的初始化变量问题

求教 C#类的初始化变量问题

資深大佬 : crella 1

在外部代码怎么用 Ngan.re_dict 来访问 Ngan 类的 Dictionary<string, Regex> redict (也需要这样来访问 Ngan.re_funcs),初衷是在某些流程里减少重复生成正则表达式。最好还是用 Dictionary 方式来储存(其他的不会…)

public class Ngan  {    public static Dictionary<Regex, Func<string, string>> re_funcs;   public static Dictionary<string, Regex> re_dict;   public Ngan()   {      // 读取正则表达式到缓存    load_re_dict();    load_re_funcs();   }      private static void load_re_dict(){    re_dict = new Dictionary<string, Regex>();    Regex quote_re = new Regex(@"[quote](.*?)[/quote]", RegexOptions.Multiline);    Regex uid_re = new Regex(@"[uid.*?](.*?)[/uid]");    Regex quoted_re = new Regex(@"[/b](.*?)[/quote]", RegexOptions.Multiline);    Regex collapse_re = new Regex(@"[collapse(=.*?)](.*?)[/collapse]", RegexOptions.Multiline);        re_dict["quote"] = quote_re;    re_dict["uid"] = uid_re;    re_dict["quoted"] = quoted_re;    re_dict["collapse"] = collapse_re;   }      private static void load_re_funcs(){        re_funcs = new Dictionary<Regex, Func<string, string>>();    // 处理回复内容     Regex reply_re = new Regex(@"Replysto.*?[uid.*?](.*?)[/uid].*?[/b]", RegexOptions.Multiline);    Func<string, string> reply_act = (con) => String.Format( "<br><i><b>回复</b>{0}</i><br>", con);    re_funcs.Add(reply_re, reply_act);        // 链接    Regex link_re = new Regex(@"[url](.*?)[/url]");    Func<string, string> link_act = (con) => {     if (!con.StartsWith("http")){      con = "http://" + con; // 自动添加 http://前缀     }     return ("<a href="" + con + "" target="_blank">链接</a>");    };    re_funcs.Add(link_re, link_act);       } // ...  } 

大佬有話說 (4)

  • 資深大佬 : geelaw

    用静态构造器,具体来说把 public Ngan 改成 static Ngan 。

  • 資深大佬 : WeaPoon

    貌似没明白你想问的,你是想问,不要总是初始化 load 么?
    如果是这意思,可以用 public static Ngan(){} 只会初始化一次。

  • 資深大佬 : WeaPoon

    1 说的对

  • 資深大佬 : lukaz

    问题问的不是很清楚,但我猜你需要的是把构造方法变成 static 的

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具