• 根据类名如何获取一个类中字段属性和字段值


    foreach (System.Reflection.PropertyInfo p in InvoiceList[i].GetType().GetProperties())
    {
    object value = p.GetValue(InvoiceList[i], null);
    string ColumnsValueString="";
    if (value!=null)
    {
    赋值:ColumnsValueString = value.ToString();
    }else{

    //赋值
    }

    详例说明:

    #region
    for (var m = 0; m < modelList.Count; m++)//循环同一个单号下的所有明细
    {
       string posLineNo = string.Empty; //单号行号信息
       DataRow drNewRow = dtNew.NewRow();//创建新行
       unit = modelList[m].规格;
       //循环读取类属性名,并将属性值赋值给dataTable中与其对应的字段名
       foreach (System.Reflection.PropertyInfo p in modelList[m].GetType().GetProperties())
       {
           //判断备注和规格型号
           string columnName = p.Name;
    
           if (columnName == "PO单号")
           {
               //备注栏中没有,则从规格型号里获取
               if (OperateType == string.Empty)
               {
                   //拆分规格中的pos单号信息
                   PosNoList = new List<PosNoInfo>();
                   SplitToArr(unit, ref PosNoList);
                   if (PosNoList.Count > 0)
                   {
                       drNewRow[columnName] = PosNoList[0].PosNo;
                       posLineNo = PosNoList[0].PosLineNo;
                       PosNoList = new List<PosNoInfo>();
                       continue;
                   }
    
               }
               //备注中只有一条po单号
               else if (OperateType == "AllSameValue")
               {
                   drNewRow[columnName] = PosNoList[0].PosNo;
                   posLineNo = PosNoList[0].PosLineNo;
                   continue;
               }
               else
               {
                   //1.po单号等于明细数 2.po单号大于1且po单号小于明细数 3.po单号大于明细数
                   var model = PosNoList.Where(a => a.LineNo == m+1).ToList();
                   if (model.Count > 0)
                   {
                       drNewRow[columnName] = model.First().PosNo;
                       posLineNo = model.First().PosLineNo;
                       continue;
                   }
    
               }
    
           }
           if (columnName == "PO单号行号")
           {
               drNewRow[columnName] = posLineNo;
               continue;
           }
           object value = p.GetValue(modelList[m], null);
           if (value != null)
           {
               drNewRow[columnName] = value.ToString();
           }
           else
           {
               drNewRow[columnName] = "";
           }
       }
       dtNew.Rows.Add(drNewRow);//追加行内容
    }
    
    #endregion
  • 相关阅读:
    jforum二次开发教程
    gitPermission denied (publickey).
    用keytool创建Keystore和Trustsotre文件只需五步
    导入数据库时报错1067 – Invalid default value for ‘字段名’
    WordPress用户角色及其权限管理编辑插件:User Role Editor汉化版
    http://blog.csdn.net/wh211212/article/details/53005321
    centos 安装 mysql
    卸载apache服务
    Cordova插件相关常用命令
    UI 交互
  • 原文地址:https://www.cnblogs.com/DreakSeeker/p/8143614.html
Copyright © 2020-2023  润新知