typeof()。GetFields不显示List <t>

时间:2018-10-10 14:43:47

标签: c# reflection system.reflection

我正在尝试上课,但无法列出所有内容。

我上课:

public class Order
{
    public string PO_NO { get; set; }
    public string APPROVED_DATE { get; set; }
    public string SPPLR_CONTACT { get; set; } //SPPLR_Mailbox
    public string PO_REMARKS { get; set; }
    public string PO_CREATION_DATE { get; set; }
    public string SPPLR_CD { get; set; }
    public string OWN_CD { get; set; }
    public string LOCATION_CODE { get; set; }
    public string LOCATION_ID { get; set; }
    public string SHIPPING_TYPE { get; set; }
    public string FREIGHT_TERMS { get; set; }
    public string PO_STATUS { get; set; }
    public string CONTACT_PERSON { get; set; }
    public string CONTACT_PERSON_MAIL { get; set; }
    public string CONTACT_PERSON_PHONE { get; set; }

    public string ATTRIBUTE01 { get; set; }
    public string ATTRIBUTE02 { get; set; }
    public string ATTRIBUTE03 { get; set; }
    public string ATTRIBUTE04 { get; set; }
    public string ATTRIBUTE05 { get; set; }
    public List<OrderDetails> OrderDetails = new List<OrderDetails>();
}

我正试图像这样“枚举”它

FieldInfo[] fields = typeof(Order).GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
foreach (var field in fields)
{
    Debug.WriteLine(field.Name + "---" + field.FieldType);
}

获取此输出:

<PO_NO>k__BackingField---System.String
<APPROVED_DATE>k__BackingField---System.String
<SPPLR_CONTACT>k__BackingField---System.String
<PO_REMARKS>k__BackingField---System.String
<PO_CREATION_DATE>k__BackingField---System.String
<SPPLR_CD>k__BackingField---System.String
<OWN_CD>k__BackingField---System.String
<LOCATION_CODE>k__BackingField---System.String
<LOCATION_ID>k__BackingField---System.String
<SHIPPING_TYPE>k__BackingField---System.String
<FREIGHT_TERMS>k__BackingField---System.String
<PO_STATUS>k__BackingField---System.String
<CONTACT_PERSON>k__BackingField---System.String
<CONTACT_PERSON_MAIL>k__BackingField---System.String
<CONTACT_PERSON_PHONE>k__BackingField---System.String
<ATTRIBUTE01>k__BackingField---System.String
<ATTRIBUTE02>k__BackingField---System.String
<ATTRIBUTE03>k__BackingField---System.String
<ATTRIBUTE04>k__BackingField---System.String
<ATTRIBUTE05>k__BackingField---System.String

列表丢失了,怎么了,或者怎么办才能将其列出来?

我正在尝试为myslef创建库,可以将类挂起,然后生成整个xml,但是由于没有得到该List(子节点),所以我无法动态地进行结构化

1 个答案:

答案 0 :(得分:0)

正如@thehenny所说,BindingFlags并不正确。我刚刚添加了BindingFlags.Public

所以代码来自:

FieldInfo[] fields = typeof(Order).GetFields(BindingFlags.NonPublic | BindingFlags.Instance );
       foreach (var field in fields)
            {
                Debug.WriteLine(field.Name + "---" + field.FieldType);
            }

收件人:

FieldInfo[] fields = typeof(Order).GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
            foreach (var field in fields)
            {
                Debug.WriteLine(field.Name + "---" + field.FieldType);
            }