Convert class properties to list of strings

How do you convert public class property names to a list of strings? Syntax is preferable in VB.NET.

I tried to do something like this:

myClassObject.GetType().GetProperties.ToList()

However, I need List (Of String)

+5
source share
2 answers

Getting the name of each property? Use LINQ to design each PropertyInfousing the LINQ method Selectby selecting MemberInfo.Name.

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

Or in VB, I think it will be:

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

Or, if you know the type you want to use in advance, you can do:

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

All Articles