在外部代码怎么用 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(@"Reply\sto.*?\[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); } // ... }
改成了这样,编译通过,执行Ngan.re_dict.Count的时候提示TypeInitializationException:"Ngan"的类型初始值设定项引发异常:在ngan4.Ngan..cctor(),定位是在re_funcs.Add(link_re, link_act);那里。
public class Ngan { public static Dictionary<Regex, Func<string, string>> re_funcs; public static Dictionary<string, Regex> re_dict; static Ngan(){ // 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; // // 链接 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); // 删除线 Regex del_re = new Regex(@"\[del\](.*?)\[\/del\]"); Func<string, string> del_act = (con) => String.Format( "<del>{0}</del>", con); re_funcs.Add(del_re, del_act); } }
![]() | 1 geelaw 2020-10-23 23:27:24 +08:00 via iPhone 用静态构造器,具体来说把 public Ngan 改成 static Ngan 。 |
![]() | 2 WeaPoon 2020-10-23 23:29:02 +08:00 貌似没明白你想问的,你是想问,不要总是初始化 load 么? 如果是这意思,可以用 public static Ngan(){} 只会初始化一次。 |
![]() | 3 WeaPoon 2020-10-23 23:32:33 +08:00 1 楼说的对 |
![]() | 4 lukaz 2020-10-23 23:33:34 +08:00 via Android 问题问的不是很清楚,但我猜你需要的是把构造方法变成 static 的 |