How to get the list of properties of a class?

后端 未结 10 1327
陌清茗
陌清茗 2020-11-21 09:48

How do I get a list of all the properties of a class?

10条回答
  •  野性不改
    2020-11-21 10:39

    Here is improved @lucasjones answer. I included improvements mentioned in comment section after his answer. I hope someone will find this useful.

    public static string[] GetTypePropertyNames(object classObject,  BindingFlags bindingFlags)
    {
        if (classObject == null)
        {
            throw new ArgumentNullException(nameof(classObject));
        }
    
            var type = classObject.GetType();
            var propertyInfos = type.GetProperties(bindingFlags);
    
            return propertyInfos.Select(propertyInfo => propertyInfo.Name).ToArray();
     }
    

提交回复
热议问题