正則表達示在ASP.NET中用來生成靜態網頁的使用

iDotNetSpace發表於2009-12-24
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。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-623595/,如需轉載,請註明出處,否則將追究法律責任。

相關文章