我正在尝试从对象中获取字段列表。我已经确认该对象正在传入此调用:
var account = SymitarInquiryDeserializer.Deserialize<SymitarAccount>(jString);
当我尝试获取字段时,它返回一个零长度数组,我无法看到我在这里做错了什么。
我的对象定义:
public class SymitarAccount
{
public int PositionalIndex { get; set; }
/// <summary>IQ: ~JID: Account/Share Id (format: 0000)</summary>
[SymitarInquiryDataFormat("ID")]
public int Id { get; set; }
/// <summary>IQ: ~JCLOSEDATE: Account/Share Closed Date where 00000000 is still open (format: YYYYMMDD)</summary>
[SymitarInquiryDataFormat("CLOSEDATE")]
public DateTime? CloseDate { get; set; }
public bool IsClosed { get; set; }
/// <summary>IQ: ~JDIVTYPE: Account/Share Div Type (format: 0)</summary>
[SymitarInquiryDataFormat("DIVTYPE")]
public int DivType { get; set; }
/// <summary>IQ: ~JBALANCE: Account/Share Balance (format: 0.00)</summary>
[SymitarInquiryDataFormat("BALANCE")]
public decimal Balance { get; set; }
/// <summary>IQ: ~JAVAILABLEBALANCE: Account/Share Avaialable Balance (format: 0.00)</summary>
[SymitarInquiryDataFormat("AVAILABLEBALANCE")]
public decimal AvailableBalance { get; set; }
}
获取字段的方法:
public static T Deserialize<T>(string str) where T : new()
{
var result = new T();
....
// Get fields of type T
var fields = typeof(T).GetFields(); //BindingFlags.Public | BindingFlags.Instance);
foreach (var field in fields)
{
....
}
....
}
有什么想法吗?
答案 0 :(得分:9)
这些不是字段而是属性。请改用GetProperties
:
var properties = typeof(T).GetProperties();
答案 1 :(得分:2)
GetFields
返回公共变量。你所追求的是你可以通过拨打GetProperties
答案 2 :(得分:0)
public void FindFieldNames<T>(List<T> data)
{
foreach (var prop in data.GetType().GetProperties())
{
Console.WriteLine($@"{prop.Name}");
}
}