zoukankan      html  css  js  c++  java
  • 模板标签替换

    生成静态页时.一般会用到标签替换,下面这个类是最近写来做个方便的替换使用..给过路的朋友分享一下,有用,maybe

    public class FormatList
    {
        
    public bool IsTestMode getset; }
        NameValueCollection nvc 
    = null;
        
    public FormatList()
        
    {
            nvc 
    = new NameValueCollection();
        }


        
    public void Add(string name, string value)
        
    {
            
    if (nvc.AllKeys.Contains(name)) nvc.Set(name, value);
            
    else nvc.Add(FormatName(name), value);
        }


        
    public void Add<FountaionEntityClass>(FountaionEntityClass source)
        
    {
            
    string FountaionEntityClassName = source.GetType().Name;
            Add
    <FountaionEntityClass>(source, FountaionEntityClassName);
        }


        
    public void Add<FountaionEntityClass>(FountaionEntityClass source, string FountaionEntityClassName)
        
    {
            FountaionEntityClassName 
    += ".";
            
    foreach (PropertyInfo pi in source.GetType().GetProperties())
            
    {
                
    try
                
    {
                    Add(FountaionEntityClassName 
    + pi.Name, pi.GetValue(source, null).ToString());
                }

                
    catch (Exception ex)
                
    { }
            }

        }



        
    private static string FormatName(string name)
        
    {
            
    return string.Format(@"\[{0}\]", name.Replace(".""\\."));
        }



        
    public string Format(string input)
        
    {
            
    if (nvc.Count == 0)
                
    return input;

            
    foreach (string key in nvc.AllKeys)
            
    {
                
    string value = nvc[key];
                
    if (value == null) value = "";

                
    if (IsTestMode) input = Regex.Replace(input, key, key + ":" + value, RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline);
                
    else input = Regex.Replace(input, key, value, RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline);

            }


            
    return input;
        }


        
    public static string Format(string input, string name, string text)
        
    {
            
    return Regex.Replace(input, FormatName(name), text, RegexOptions.IgnoreCase | RegexOptions.Compiled);
        }



    }

    无论你添加什么标签,最终都是以String的格式对源进行replace,这里还包括一个比较好用的方法,就是用泛型+反射去增加标签,这样可以减少比较繁琐的功夫,一次性把整个类都添加进去了
    通常使用的标签为   [标签名]  或 [类名.字段名]...

  • 相关阅读:
    JS截取文件后缀名
    百度地图API示例:使用vue添加删除覆盖物
    AttributeError: module 'tensorflow' has no attribute 'sub'
    ModuleNotFoundError: No module named 'numpy.core._multiarray_umath' ImportError: numpy.core.multiarray failed to import
    千锋很火的SpringBoot实战开发教程视频
    sublime text3 3176 注册码 License
    linux后台运行jar程序
    使用eclipse的SVN连接码云
    关于git上传文件的一个小问题
    js正则表达式,密码长度要大于6位,由数字和字母组成
  • 原文地址:https://www.cnblogs.com/yans/p/1216187.html
Copyright © 2011-2022 走看看