简体   繁体   中英

Convert class properties into a list of strings

How do you convert the names of public properties of a class into a list of strings? Syntax preferably in VB.NET.

I was trying to do something like this:

myClassObject.GetType().GetProperties.ToList()

However, I need a List(Of String)

Getting the name of each property? Use LINQ to project each PropertyInfo using the LINQ Select method, selecting the MemberInfo.Name property.

myClassObject.GetType().GetProperties().Select(p => p.Name).ToList()

Or in VB, I think it would be:

myClassObject.GetType.GetProperties.Select(Function (p) p.Name).ToList
var propertyNames = myClassObject.GetType().GetProperties().Select(p => p.Name).ToList();

或者,如果您事先知道要使用的类型,则可以执行以下操作:

var propertyNames = typeof(ClassName).GetProperties().Select(p => p.Name).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM