repeater 模擬器 in winform


需求

有一個這樣的需求,根據給定的數據源和html模板生成html,強大的All in one code中的代碼不能滿足需求,所以就有了RepeaterSimulate.

其中涉及:反射、泛型、正則表達式等基礎知識。

 

RepeaterSimulate使用

            RepeaterSimulate r = new RepeaterSimulate();
            r.ItemTemplate = "<div>#Name#<div><div>#Age#<div>";
            List<TestModel> tm = new List<TestModel>() { 
               new TestModel(){Name="zhangsan",Age=11},
               new TestModel(){Name="lisi",Age=12},
               new TestModel(){Name="wangwu",Age=13},
               new TestModel(){Name="sunliu",Age=31}
            };
            string html = r.GenerateHTML<TestModel>(tm);
            Console.WriteLine(html);
            Console.Read();

效果如圖:

image

 

RepeaterSimulate實現

模擬器中的GenerateHTML方法:

        public string GenerateHTML<T>(object obj)
        {
            StringBuilder sb = new StringBuilder();
            IEnumerable<T> enumerable = obj as IEnumerable<T>;
            MemberInfo[] myMemberInfo;
            // Get the type of 'MyClass'.
            Type myType = typeof(T);
            // Get the information related to all public member's of 'MyClass'. 
            myMemberInfo = myType.GetMembers();
            string regexText = "#\\w{1,}#";
            Match mt = Regex.Match(this.ItemTemplate, regexText);
            List<string> results = RegularHelper.GetResult(mt);
            foreach (var item in enumerable)
            {
                string tempStr = this.ItemTemplate;
                for (int i = 0; i < myMemberInfo.Length; i++)
                {
                   
                    if (myMemberInfo[i].MemberType.ToString() == "Property")
                    {
                        if (results.Contains(myMemberInfo[i].Name))
                        {
                            
                            object v = myType.InvokeMember(myMemberInfo[i].Name,
           BindingFlags.DeclaredOnly |
           BindingFlags.Public | BindingFlags.NonPublic |
           BindingFlags.Instance | BindingFlags.GetProperty, null, item, null);
                            tempStr = tempStr.Replace("#" + myMemberInfo[i].Name + "#", v.ToString());                         
                        }                     
                    }
                 
                }
                sb.Append(tempStr);
            }
            return sb.ToString();
        }

其中的#\\w{1,}#匹配對象為”#中間為字符,字符數量大於1個#”,這里約定好被綁定的數據源的屬性名在兩個#之間,當然也可以類似aspx<%#神馬的 %>

遍歷正則匹配到的屬性:

    public class RegularHelper
    {
        private static List<string>  Result {get;set;}
        public static List<string> GetResult(Match mt)
        {
            List<string> r = new List<string>();
            GetMatchValues(mt,r);
            return r;
        }
        private static void GetMatchValues(Match mt, List<string> r)
        {
            r.Add(mt.Value.TrimStart('#').TrimEnd('#'));
            if (mt.NextMatch().Length > 0)
                GetMatchValues(mt.NextMatch(),r);
        }
    }

上面的GetMatchValues遞歸遍歷出所有匹配的對象。

 

思考題

支持這種寫法#Name.substring(0,12)+”……”#

下載試用


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM