How to loop on field names of a class

前端 未结 5 503
甜味超标
甜味超标 2021-01-03 20:54

I have got a class which contains more then 150 fields. i need the name of fields (not value) in an array.

because its very hard and not a good approach to write 150

5条回答
  •  温柔的废话
    2021-01-03 21:50

    Worked for me

    var t = typeof(YOURTYPE);
    
    var allfieldNames = new List(t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Select(x => x.Name));
    

    // t for Type

提交回复
热议问题