zoukankan      html  css  js  c++  java
  • 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)+”……”#

    下载试用

  • 相关阅读:
    6 November in 614
    4 November in ss
    标准模板库(STL)
    类模板
    函数模板和模板函数
    关于“宏定义”的作用范围
    运算符重载
    内存分配和释放的函数
    数据库恢复的基础是利用转储的冗余数据
    在局域网络内的某台主机用ping命令测试网络连接时发现网络内部的主机都可以连同,而不能与公网连通,问题可能是
  • 原文地址:https://www.cnblogs.com/iamzhanglei/p/2342903.html
Copyright © 2011-2022 走看看