• 正则表达示在ASP.NET中用来生成静态网页的使用


     static void Main() {
                // Create proxy and request a service
                //Proxy proxy = new Proxy();
                //proxy.Request();
                // Wait for user
                GenerationStaticPage p = new GenerationStaticPage();
                string pagecontent = p.GenerationPage();
                Console.Write(pagecontent);
                Console.Read();
               
            }
            public class GenerationStaticPage
            {
                #region 属性
                public string MemberName
                {
                    get
                    {
                        return "Vodgo";
                    }
                    set
                    {
                        //
                    }

                }
                public string MeetingName
                {
                    get
                    {
                        return "Productor page";
                    }
                    set
                    {
                        //
                    }

                }

                public string RoomName
                {
                    get
                    {
                        return "24-1";
                    }
                    set
                    {
                        //
                    }

                }

                public string BeginTime
                {
                    get
                    {
                        return "2007-12-1 12:01:29";
                    }
                    set
                    {
                        //
                    }

                }
                #endregion

                public string GenerationPage()
                {

                    //读取模板的内容(方法自己写吧)
                    string content = "你好,#?MemberName?#请出席会议:#?MeetingName?#。地点:#?RoomName?#。时间:#?BeginTime?#。";
                    StringBuilder builder = new StringBuilder(content);
                    string pattern = @"#\?(?'property'\S+?)\?#";
                    return Regex.Replace(content, pattern, new MatchEvaluator(RegexMatchEvaluation), RegexOptions.ExplicitCapture);
                }

                private string RegexMatchEvaluation(Match match)
                {
                    //get the property name (named group of the regex)
                    string propertyName = match.Groups["property"].Value;

                    //try to get a property handle from the business object
                    PropertyInfo pi = this.GetType().GetProperty(propertyName);

                    //do not replace anything if no such property exists
                    if (pi == null)
                    {
                        return match.Value;
                    }

                    //return the property value
                    object propertyValue = pi.GetValue(this, null);
                    if (propertyValue != null)
                    {
                        return propertyValue.ToString();
                    }
                    else
                    {
                        return string.Empty;
                    }
                }

            }     
    比较模板内容:
    你好,#?MemberName?#请出席会议:#?MeetingName?#。地点:#?RoomName?#。时间:#?BeginTime?#。
    运行结果:
    你好,Vodgo请出席会议:Productor page。地点:24-1。时间:2007-12-1 12:01:29。


  • 相关阅读:
    Mac 配置自定义执行文件 pull.sh,push.sh
    vue-element-admin列表管理
    使用elementui图标
    Mac brew 启动php
    VUE , 表单中如何用获取接口数据的select
    Mac 如何关闭PHPstorm,双击shift快捷键
    spectacle 很好用的应用分屏工具Mac
    面对灵活的配置如何建表,使用json
    mac 下灵活管理node版本
    Node Sass version 6.0.0 is incompatible with^4.0.0
  • 原文地址:https://www.cnblogs.com/sgciviolence/p/1154351.html
Copyright © 2020-2023  润新知