• C# 会自动替换 变量 把形如 "{{varName}}" 替换成对应的数值


    C# 会自动替换 变量   把形如 "{{varName}}" 替换成对应的数值

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Threading.Tasks;
    
    namespace Common
    {
        public class DynamicVariableHelper
        {
            /// <summary>
            /// 会自动替换 变量   把形如 "{{varName}}" 替换成对应的数值
            /// </summary>
            public static void ReplaceFileVar(string srcFile, string targetFile)
            {
                /*
                * 设置webconfig连接字符串
                */
                string webconfigpath = Path.Combine("./", srcFile);
                //修改第一个数据库连接
                string webcofnigstring2 = File.ReadAllText(webconfigpath);
    
                webcofnigstring2 = ReplaceStringVar(webcofnigstring2);
    
                //循环替换所有的 {{DBSERVER}} 这种变量 效率比较低,改成上面的replace
                //foreach (Match item in mat)
                //{
                //    Console.WriteLine(item.Groups[1]);
                //    var name = item.Groups[1].Value;
                //    if (! string.IsNullOrEmpty( InstallContext.Get(name)))
                //    {
                //         webcofnigstring2.Replace(@"{{"+ name + "}}", InstallContext.Get(name));
                //    }
                //}
    
                File.WriteAllText(targetFile, webcofnigstring2);
            }
    
    
            /// <summary>
            /// 会自动替换 变量   把形如 "{{varName}}" 替换成对应的数值
            /// </summary>
            public static string ReplaceStringVar(string str)
            {
                Regex reg = new Regex(@"\{\{(.*?)\}\}");
                //var mat = reg.Matches(webcofnigstring2);
    
                str = reg.Replace(str,
                    new MatchEvaluator(m =>
                         InstallContext.Get(m.Groups[1].Value) == string.Empty ? m.Value : InstallContext.Get(m.Groups[1].Value)
                    ));
                return str;
            }
        }
    
        public static class InstallContext
        {
            private static Dictionary<string, string> kvs = new Dictionary<string, string>();
    
            public static string Get(string index)
            {
                if (kvs.ContainsKey(index))
                {
                    return kvs[index];
                }
                else
                {
                    return string.Empty;
                }
            }
            public static void Set(string index, string value)
            {
                kvs[index] = value;
            }
    
    
    
            //private static InstallContext instance = new InstallContext();
    
            //private InstallContext()
            //{
    
            //}
    
            //public static InstallContext GetInstance()
            //{ 
            //    return instance;
            //}
    
        }
    }
  • 相关阅读:
    淘票票项目总结
    淘票票 Model制作过程
    黄金梅丽号第四次讨论会议
    淘座座用户需求调查问卷的调查结果
    淘座座用户需求调查问卷
    淘座座需求分析报告
    淘座座软件工程技术说明书
    淘座座软件项目计划书
    利用hutool配置发送邮件的问题 及 阿里企业邮箱526 Authentication failure 错误问题
    DispatcherServlet和ContextLoaderListener,还有spring+servlet3.0 无web.xml启动问题
  • 原文地址:https://www.cnblogs.com/xiaoruilin/p/16082481.html
Copyright © 2020-2023  润新知