简体   繁体   English

将类属性转换为字符串列表

[英]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. 语法最好在VB.NET中。

I was trying to do something like this: 我试图做这样的事情:

myClassObject.GetType().GetProperties.ToList()

However, I need a List(Of String) 但是,我需要一个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. 使用LINQ使用LINQ Select方法投影每个PropertyInfo ,选择MemberInfo.Name属性。

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

Or in VB, I think it would be: 或者在VB中,我认为它将是:

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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