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

  • 相关阅读:
    Android中的sp与wp
    MTK
    linux kernel文件系统启动部分
    Java项目构建基础之统一结果
    线程和线程池的学习
    SpringBoot 中MyBatis的配置
    MyBatis中使用Map传参——返回值也是Map
    OAuth2的学习
    Java 跨域问题
    Spring Cloud 中的 eureka.instance.appname和spring.application.name 意思
  • 原文地址:https://www.cnblogs.com/raorao1994/p/5090379.html
Copyright © 2011-2022 走看看