zoukankan      html  css  js  c++  java
  • 通过反射得到object[]数组的类型并且的到此类型所有的字段及字段的值

    private string T_Account(object[] list)
    {
    StringBuilder code = new StringBuilder();
    //得到数据类型
    Type t = list[0].GetType();
    List<string> str = new List<string>();
    //得到类型的所有字段
    FieldInfo[] fields = t.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
    code.AppendLine("<table class='mytable'><thead><tr>");
    foreach (FieldInfo fi in fields)
    {
    //提取反编译后字段名
    Match m = Regex.Match(fi.Name, @"(?<=\<).+(?=\>)");
    code.Append("<td>").Append(m.Value).AppendLine("</td>");
    str.Add(m.Value);
    }
    code.AppendLine("</tr>");
    for(int i=0;i<list.Length;i++)
    {
    object ob=Activator.CreateInstance(t);
    code.AppendLine("<tr>");
    foreach (FieldInfo fi in fields)
    {
    //提取反编译后字段名
    Match m = Regex.Match(fi.Name, @"(?<=\<).+(?=\>)");
    //得到类相应字段的值
    code.Append("<td>").Append(fi.GetValue(list[0])).AppendLine("</td>");
    str.Add(m.Value);
    }
    code.AppendLine("</tr>");
    }
    code.AppendLine("</table>");
    string s = code.ToString();
    return code.ToString();
    }

    资料获取方式,关注公总号RaoRao1994,查看往期精彩-所有文章,即可获取资源下载链接

    更多资源获取,请关注公总号RaoRao1994

  • 相关阅读:
    Class 'ViewCell' has no initializers
    xcode:关于Other Linker Flags
    NSString编码形式是UTF–16
    手写识别方案汇总
    将一个 view 进行截图
    程序员最核心的竞争力是什么?
    sqlite3
    创建NAT 64网络
    Snagit安装步骤
    cnblogs插件jiathis
  • 原文地址:https://www.cnblogs.com/raorao1994/p/5090379.html
Copyright © 2011-2022 走看看