Exemplo n.º 1
0
        /// <summary>
        /// 表达式解析
        /// </summary>
        /// <param name="template"></param>
        /// <param name="pairs"></param>
        /// <returns></returns>
        public static string Parse(string template, SafeMap <object> pairs, ELOption option = default(ELOption))
        {
            string result = new string(template.ToCharArray());

            // 忽略不匹配项
            if (option.Ignore)
            {
                // 需要优化为,匹配到 \$\{\S*?\} 后按照匹配到的内容作为Key在Map中寻找Value替换
                foreach (var key in pairs.Keys)
                {
                    Regex regex = new Regex(@"\$\{" + key + @"\}");                                                                                        // 如果Key也是正则表达式??
                    result = regex.Replace(result, pairs[key].ToString());
                    result = regex.Replace(result, new MatchEvaluator(m => pairs.ContainsKey(key) ? pairs.UnSafeGet(key).ToString() : m.Groups[1].Value)); // ?? 这里的m. m.Groups[1] 难道指的是一个匹配项,而不是所有匹配项
                }
            }
            // 将未找到匹配项的占位符删除
            else
            {
                // 需要优化为,匹配到 \$\{\S*?\} 后按照匹配到的内容作为Key在Map中寻找Value替换
                foreach (var key in pairs.Keys)
                {
                    Regex regex = new Regex(@"\$\{" + key + @"\}");
                    result = regex.Replace(result, pairs[key].ToString()); //
                }
            }
            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 通过映射表(字典)来创建SafeMap
        /// </summary>
        /// <typeparam name="TV">保存的类型</typeparam>
        /// <param name="pairs">映射表(字典)</param>
        /// <returns></returns>
        public static SafeMap <TV> New <TV>([Required] Dictionary <string, TV> pairs)
        {
            SafeMap <TV> map = new SafeMap <TV>
            {
                kvs = pairs
            };

            return(map);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 测试模板替换,vs2017->鼠标方法名->右键交互执行->需要把那些依赖的方法和using都交互执行过才行
        /// </summary>
        public static void Test()
        {
            SafeMap <object> map = new SafeMap <object>();

            map["username"] = "******";
            map["password"] = "******";

            Console.WriteLine(ReplacePlaceholder("${username}: ${password}", map));
        }
Exemplo n.º 4
0
        /// <summary>
        /// 采用JSON字符串初始化对象
        /// 未测试
        /// </summary>
        /// <param name="json"></param>
        public static SafeMap <TV> New <TV>(string json)
        {
            SafeMap <TV> map = new SafeMap <TV>();

            JObject jo = (JObject)JsonConvert.DeserializeObject(json);

            foreach (var prop in jo)
            {
                map.kvs.Add(prop.Key, prop.Value.Value <TV>());
            }
            return(map);
        }
Exemplo n.º 5
0
        /// <summary>
        /// 占位符替换: ${}
        /// ${TagName} 如果TagName找不到将整体消去${TagName}.
        /// "${Date} ${NotFoundTagName}end" -> "2018-11-28 end".
        /// </summary>
        /// <param name="template">模板字符串</param>
        /// <returns></returns>
        public static string ReplacePlaceholder <TValue>(string template, SafeMap <TValue> pairs)
        {
            string result = new string(template.ToCharArray());

            // 需要优化为,匹配到 \$\{\S*?\} 后按照匹配到的内容作为Key在Map中寻找Value替换
            foreach (var key in pairs.Keys)
            {
                Regex regex = new Regex(@"\$\{" + key + @"\}");
                result = regex.Replace(result, pairs[key].ToString()); //
            }
            return(result);
        }
Exemplo n.º 6
0
        /// <summary>
        /// 占位符替换: ${}.
        /// ${TagName} 如果TagName找不到将保留${TagName}.
        /// "${Date} ${NotFoundTagName}" -> "2018-11-28 ${NotFoundTagName}"
        /// Created by Wagsn on 2018/11/28 11:29.
        /// </summary>
        /// <param name="template">模板字符串</param>
        /// <returns></returns>
        public static string ReplacePlaceholderByIgnore <TValue>(string template, SafeMap <TValue> pairs)
        {
            string result = new string(template.ToCharArray());

            // 需要优化为,匹配到 \$\{\S*?\} 后按照匹配到的内容作为Key在Map中寻找Value替换
            foreach (var key in pairs.Keys)
            {
                Regex regex = new Regex(@"\$\{" + key + @"\}");                                                                                        // 如果Key也是正则表达式
                result = regex.Replace(result, pairs[key].ToString());
                result = regex.Replace(result, new MatchEvaluator(m => pairs.ContainsKey(key) ? pairs.UnSafeGet(key).ToString() : m.Groups[1].Value)); // ?? 这里的m. m.Groups[1] 难道指的是一个值,而不是所有匹配项
            }
            return(result);
        }
Exemplo n.º 7
0
 /// <summary>
 /// 占位符替换: ${}
 /// ${TagName} 如果TagName找不到将整体消去${TagName}.
 /// "${Date} ${NotFoundTagName}end" -> "2018-11-28 end".
 /// </summary>
 /// <param name="template">模板字符串</param>
 /// <returns></returns>
 public static string ReplacePlaceholder <TValue>(string template, SafeMap <TValue> pairs)
 {
     return(elRagex.Replace(template, new MatchEvaluator(m => pairs[m.Groups[2].ToString()]?.ToString())));
 }