• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

[C#]遍历对象属性、遍历结构体字段-fqzhang

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

[C#] 遍历对象属性、遍历结构体字段

[C#] 遍历对象属性、遍历结构体字段

 
最近需要将原有的SQL数据库中的数据导入到MongoDB中
 
同事提供了对象的结构体,该结构体中包含嵌套的结构体
 
我需要得到结构中的每个字段的值
 
没其他方法 ,遍历吧 同学
 
1.遍历结构体:
 
其中 ResumeInfo为结构体[并且内嵌了结构体]
 
ResumeInfo resume1 = new ResumeInfo();
FieldInfo[] fieldInfos = typeof(ResumeInfo).GetFields();
int FieldCount = fieldInfos.Length;
for (int i = 0; i < FieldCount; i++)
{
FieldInfo FiledFirstLayer = fieldInfos[i];
object Value = FiledFirstLayer.GetValue(resume1);
if (Value != null && Value.ToString() != "0")
{
//新建子节点文档
Document ChildDoc = new Document();
FieldInfo[] tmpFieldInfos =  FiledFirstLayer.GetValue(resume1).GetType().GetFields();
  int tmpFiledCount = tmpFieldInfos.Length;
for (int j = 0; j < tmpFiledCount; j++)
{
ChildDoc[tmpFieldInfos[j].Name] = new Document() .Add("label", null) .Add("value", null) .Add("type", null) .Add("model", null); } Resume[FiledFirstLayer.Name] = ChildDoc;
}
else
{
Resume[FiledFirstLayer.Name] = new Document() .Add("label", null) .Add("value", null) .Add("type", null) .Add("model", null);
} }
 
1.遍历类:
 
其中 Resume为一个类
 
PropertyInfo[] properties = typeof(Resume).GetProperties();
          foreach (PropertyInfo p in properties)
          {
              NewResume[p.Name] = p.GetValue(ResumeInfo, null);
          }

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
【转】C#Async/Await异步编程中的最佳做法发布时间:2022-07-10
下一篇:
C# 建行聚合支付发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap