C# 通过反射获取类字段名和值并加入到字典中(包含递归获取)
//测试类
public class HKAddvisitor1
{
public string code { set; get; }
public string msg { set; get; }
public string data { set; get; }
}
//调用
HKAddvisitor1 hKAddvisitor1 = new HKAddvisitor1()
{
code = "1",
data = "2",
msg = "搞定",
};
GetFields(hKAddvisitor1);
//基础方法
Dictionary<string, string> GetFields<T>(T t)
{
Dictionary<string, string> fields = new Dictionary<string, string>();
var type = t.GetType().GetProperties();
foreach (PropertyInfo item in type)
{
var typeName = item.PropertyType.Name;
if (typeName == "Nullable`1")
{
typeName = item.PropertyType.GenericTypeArguments.FirstOrDefault().Name;
}
Console.WriteLine("{0}-->{1}", item.Name, item.GetValue(t));
}
return fields;
}
//递归测试类
public class HKAddvisitor1
{
public string sucode { set; get; }
public string sumsg { set; get; }
public string sudata { set; get; }
}
public class HKAddvisitor
{
public string code { set; get; }
public string msg { set; get; }
public HKAddvisitor1 data { set; get; }
}
//递归调用
HKAddvisitor hKAddvisitor = new HKAddvisitor()
{
code = "1",
msg = "搞定",
data = new HKAddvisitor1
{
sucode = "2",
sumsg = "heheh",
sudata = "dsa"
}
};
Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
GetInfoPropertys(hKAddvisitor, keyValuePairs);
foreach (var item in keyValuePairs)
{
Console.WriteLine("属性名:{0},属性值:{1}", item.Key, item.Value);
}
//递归获取
void GetInfoPropertys(object objInfo, Dictionary<string, string> keyValuePairs)
{
StringBuilder strB = new System.Text.StringBuilder();
if (objInfo == null) return;
Type tInfo = objInfo.GetType();
PropertyInfo[] pInfos = tInfo.GetProperties();
if (tInfo.IsGenericType)
{
System.Collections.ICollection Ilist = objInfo as System.Collections.ICollection;
if (Ilist != null)
{
strB.AppendFormat("集合⼦属性{0}<br/>", Ilist.Count);
foreach (object obj in Ilist)
{
GetInfoPropertys(obj, keyValuePairs);
}
}
else
{
strB.Append("泛型集合为空<br/>");
}
return;
}
foreach (PropertyInfo pTemp in pInfos)
{
string Pname = pTemp.Name;
string pTypeName = pTemp.PropertyType.Name;
object Pvalue = pTemp.GetValue(objInfo, null);
if (pTemp.PropertyType.IsValueType || pTemp.PropertyType.Name.StartsWith("String"))
{
string value = (Pvalue == null ? "" : Pvalue.ToString());
keyValuePairs.Add(Pname, value);
Console.WriteLine("属性名:{0},属性类型:{1},属性值:{2}<br/>", Pname, pTypeName, value);
strB.AppendFormat("属性名:{0},属性类型:{1},属性值:{2}<br/>", Pname, pTypeName, value);
}
else
{
//string value = Pvalue == null ? "" : Pvalue.ToString();
//Console.WriteLine("<br/><b>⼦类</b>,属性名:{0},属性类型:{1},属性值:{2}<br/>", Pname, pTypeName, value);
//strB.AppendFormat("<br/><b>⼦类</b>,属性名:{0},属性类型:{1},属性值:{2}<br/>", Pname, pTypeName, value);
//keyValuePairs.Add(Pname, value);
//strB.Append("----------------------------------------------<br/>");
GetInfoPropertys(Pvalue, keyValuePairs);
}
}
}
//拓展
https://www.cnblogs.com/bmyblogs/p/9346843.html (判断属性类型)
https://wenku.baidu.com/view/6b904c485bfafab069dc5022aaea998fcc2240e4.html (递归)
最新文章
- 5.首次登陆与在线求助man page
- HTML5 web Form表单验证实例
- springmvc 注解总结
- Android ImageView图片自适应 (转)
- Decision Boundaries for Deep Learning and other Machine Learning classifiers
- 软件设计之UML—UML中的六大关系
- photoswipe
- MyMVC框架的使用
- [译]Java 垃圾回收的监控和分析
- 运动检测(前景检测)之(一)ViBe
- compute post expression
- InnoDB: ERROR: the age of the last checkpoint
- html5 javascript 小型计算器
- 从Unity中的Attribute到AOP(七)
- vue ajax获取数据的时候,如何保证传递参数的安全或者说如何保护api的安全
- C#导入Excel、Excel导入、导入.xls 、导入.xlsx、Excel2003版本、Excel2007版本
- WPF ListView点击删除某一行并获取绑定数据
- from __future__ import print_function的作用
- 解决微信小程序ios端滚动卡顿的问题
- 电子书下载:Delphi XE 5 移动开发入门手册(完整版)
热门文章
- 社论 22.10.14 区间在线去重k小
- 各类数据库写入Webhsell总结
- 【每日一题】【使用list&;使用辅助栈实现】2022年2月11日-NC90 包含min函数的栈
- JavaScript入门⑧-事件总结大全
- 进击的K8S:Kubernetes基础概念
- python连接MySQL数据库实现(用户登录测试功能)pymysql
- Spring学习笔记 - 第二章 - 注解开发、配置管理第三方Bean、注解管理第三方Bean、Spring 整合 MyBatis 和 Junit 案例
- 连接MySql时提示%d format: a number is required, not str
- cs231n__4.1 Backpropagation and Neural Network
- Http请求接口