Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert class properties into a list of strings

Tags:

c#

.net

vb.net

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)

like image 627
Prabhu Avatar asked Jul 23 '12 19:07

Prabhu


2 Answers

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
like image 53
Jon Skeet Avatar answered Nov 15 '22 16:11

Jon Skeet


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();
like image 38
Lee Avatar answered Nov 15 '22 15:11

Lee