[Test]
public void Test()
{
var result = new Dictionary<string, string>();
var creditCard = new PbocCreditCard()
{
rh_bad_credit_limit = 100
};
if (creditCard != null)
{
result = creditCard.GetType().GetProperties().ToDictionary(p => p.Name, p => p.GetValue(creditCard)?.ToString());
}
var dt = new DateTime();
var dic = dt.GetType().GetProperties().ToDictionary(q => q.Name, q => q.GetValue(dt).ToString());
var json = JsonConvert.SerializeObject(dic);
json = JsonConvert.SerializeObject(result);
}
c#类属性反射成字典或其他集合
本文展示了C#中如何使用反射获取对象属性并进行序列化为JSON的示例代码。通过`PbocCreditCard`类的属性获取与`DateTime`类型的属性转换,演示了如何构建字典并利用JsonConvert进行序列化操作。
摘要由CSDN通过智能技术生成