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

  • 相关阅读:
    HDU 2098 分拆素数和
    HDU 2034 *人见人爱A-B
    HDU 1236 排名(Microsoft_zzt)
    HDU 5702 Solving Order
    HDU 2033 人见人爱A+B
    HDU 2029 Palindromes _easy version
    HDU 2021 发工资咯:)
    HDU 2071 Max Num
    HDU 2039 三角形
    页面使用element-tree
  • 原文地址:https://www.cnblogs.com/raorao1994/p/5090379.html
Copyright © 2011-2022 走看看