博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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

Posted on 2009-12-24 18:34  elgt  阅读(251)  评论(1编辑  收藏  举报
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。