V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
crella
V2EX  ›  C#

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

  •  
  •   crella · 2020-10-23 23:20:05 +08:00 · 3265 次点击
    这是一个创建于 1252 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在外部代码怎么用 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);
    			
    		}
    // ...
    	}
    
    第 1 条附言  ·  2020-10-24 00:01:50 +08:00

    改成了这样,编译通过,执行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);
    		}
    	}
    
    第 2 条附言  ·  2020-10-24 00:04:30 +08:00
    搞定了,忽略第一条 Append 。
    4 条回复    2020-10-23 23:33:34 +08:00
    geelaw
        1
    geelaw  
       2020-10-23 23:27:24 +08:00 via iPhone
    用静态构造器,具体来说把 public Ngan 改成 static Ngan 。
    WeaPoon
        2
    WeaPoon  
       2020-10-23 23:29:02 +08:00
    貌似没明白你想问的,你是想问,不要总是初始化 load 么?
    如果是这意思,可以用 public static Ngan(){} 只会初始化一次。
    WeaPoon
        3
    WeaPoon  
       2020-10-23 23:32:33 +08:00
    1 楼说的对
    lukaz
        4
    lukaz  
       2020-10-23 23:33:34 +08:00 via Android
    问题问的不是很清楚,但我猜你需要的是把构造方法变成 static 的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2193 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 16:13 · PVG 00:13 · LAX 09:13 · JFK 12:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.