zoukankan      html  css  js  c++  java
  • 正则表达示在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。


  • 相关阅读:
    Unbuntu--安装VMware Tools
    方法引用的基本使用
    Stream流
    Stream流的常用方法
    枚举
    编程式路由导航
    向路由组件传递数据
    缓存路由组件
    嵌套路由
    基本路由
  • 原文地址:https://www.cnblogs.com/sgciviolence/p/1154351.html
Copyright © 2011-2022 走看看