How to loop on field names of a class

前端 未结 5 511
甜味超标
甜味超标 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:32

    try

        public static string[] GetFieldNames(Type t)
        {
            FieldInfo[] fieldInfos = t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            return fieldInfos.Select(x => x.Name).ToArray();
        }
    

提交回复
热议问题