首页 > 编程语言 >c#遍历一个对象的字段信息

c#遍历一个对象的字段信息

时间:2023-02-02 15:46:09浏览次数:54  
标签:遍历 string get c# WordData 对象 set props public

c#遍历对象字段

场景:有一个对象作为导出word段落的数据、每一个字段就代表一个段落,可以对相应段落数据设置样式(字体、颜色、加粗……)

参考文献:(12条消息) C#获取实体类字段信息PropertyInfo,字段名称,字段值,字段属性标签_棉晗榜的博客-CSDN博客_c# t实体获取某个字段的值

1、对象

   public class WordData
    {
        /// <summary>
        /// 教室名称
        /// </summary>
        public string cName { get; set; }
        /// <summary>
        /// 教室编号
        /// </summary>
        public string cId { get; set; }
        /// <summary>
        /// 备注
        /// </summary>
        public string Rank { get; set; }
        /// <summary>
        /// 联系电话
        /// </summary>
        public string Phone { get; set; }
        /// <summary>
        /// 二维码
        /// </summary>
        public FileStream QRStream { get; set; }
    }

 2、获取属性中的段落信息

//获取这个类的字段信息
PropertyInfo[] props = typeof(WordData).GetProperties();//实体的字段列表

 3、遍历对像相应字段(一个字段代表一个段落,一行)做逻辑处理

 FileStream fileStream=null;//二维码  
 string Text = "";//文本
 for (var j = 0; j < props.Count(); j++)
  {
      //如果字段名不是QRStream
      if (props[j].Name != "QRStream")
      {
           Text = props[j].GetValue(w) as string;
      }
      else
      {
              //图片stream
              fileStream = props[j].GetValue(w) as FileStream;
      }
           ……………………  接下来对其做逻辑处理
   }

4、最终效果

数据是这个样子的

string FilePath = System.AppDomain.CurrentDomain.BaseDirectory + "Img";
FilePath = FilePath + "/QR.png";
FileStream fs = new FileStream(FilePath, FileMode.OpenOrCreate, FileAccess.Read);
WordData w = new WordData() { cName= "[房间1]" , cId="14985"+1,Rank= "请扫描二维码进行故障报修。关注公众号,可随时了解当前进度",Phone= "报修热线:14541524124", QRStream=fs};

通过逻辑处理后导出word效果 :(WordData提供的文本和图片stream)

导出word使用NPOI插件,参考博客:.Net Word操作之NPOI - じ逐梦 - 博客园 (cnblogs.com)

 

标签:遍历,string,get,c#,WordData,对象,set,props,public
From: https://www.cnblogs.com/ZhuMeng-Chao/p/17086229.html

相关文章

  • 什么是面向对象的眼光
    从面向对象的眼光来看,开发者希望从自然的认识、使用角度来定义和使用类。也就是说,开发者希望直接对客观世界进行模拟:定义一个类,对应客观世界的哪种事物;业务需要关心这个事......
  • [LeetCode]Minimum Path Sum
    QuestionGivenamxngridfilledwithnon-negativenumbers,findapathfromtoplefttobottomrightwhichminimizesthesumofallnumbersalongitspath.N......
  • [LeetCode]Unique Paths
    QuestionArobotislocatedatthetop-leftcornerofamxngrid(marked‘Start’inthediagrambelow).Therobotcanonlymoveeitherdownorrightatany......
  • [LeetCode]Permutation Sequence
    QuestionTheset[1,2,3,…,n]containsatotalofn!uniquepermutations.Bylistingandlabelingallofthepermutationsinorder,Wegetthefollowingsequen......
  • [LeetCode]Length of Last Word
    QuestionGivenastringsconsistsofupper/lower-casealphabetsandemptyspacecharacters​​​''​​,returnthelengthoflastwordinthestring.Ifthe......
  • [LeetCode]Insert Interval
    QuestionGivenasetofnon-overlappingintervals,insertanewintervalintotheintervals(mergeifnecessary).Youmayassumethattheintervalswereinitial......
  • [LeetCode]Merge Intervals
    Question本题难度Hard。排序+双指针【复杂度】时间O(Nlog(N))空间O(N)【思路】先按照每个元素的​​​start​​​从小到大进行排序。然后利用双指针法,设置区间​​......
  • [LeetCode]Maximum Subarray
    QuestionFindthecontiguoussubarraywithinanarray(containingatleastonenumber)whichhasthelargestsum.Forexample,giventhearray[-2,1,-3,4,-1,2,1......
  • [LeetCode]Spiral Matrix
    QuestionGivenamatrixofmxnelements(mrows,ncolumns),returnallelementsofthematrixinspiralorder.Forexample,Giventhefollowingmatrix:[[1......
  • Webots下载安装 + Pycharm联调
    第一章Webots安装目录第一章Webots安装前言一、Webots是什么?二、WebotsR2022b安装1.下载2.安装3.Pycharm作为IDE3.1设置环境变量3.2Webots设置总结前言本系列......