C#中:如何返回一个对象的所有属性名称的列表? [英] C#: how to return a list of the names of all properties of an object?

查看:183
本文介绍了C#中:如何返回一个对象的所有属性名称的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类:

 公共类Foo
{
    公共IEnumerable的<串GT; stst_soldToALTKN {搞定;组; }
    公众诠释SID {搞定;组; }
    公共字符串stst_LegalName {搞定;组; }
    公共字符串stst_TradeName {搞定;组; }
    公共字符串stst_StreetAddress {搞定;组; }
}

有没有我可以调用,将返回一个列表/ IEnumerable的每个属性???

的名称的方法

例如:

 等等foo1 =新的Foo();
IEnumerable的<串GT; foo1List = GetAllPropertyNames(foo1);
foo1List.ToList();

结果:'stst_soldToALTKN','SID','stst_LegalName','stst_TradeName','stst_StreetAddress


解决方案

  VAR propNames = foo1.GetType()
                    .GetProperties()
                    。选择(PI => pi.Name)

I have a class:

 public class foo
{
    public IEnumerable<string> stst_soldToALTKN { get; set; }
    public int sId { get; set; }        
    public string stst_LegalName { get; set; }
    public string stst_TradeName { get; set; }
    public string stst_StreetAddress { get; set; }
}

Is there a method I can call that would return a list/ienumerable of the names of each property???

For Example:

blah foo1 = new foo();
ienumerable<string> foo1List = GetAllPropertyNames(foo1);
foo1List.ToList();

Result: 'stst_soldToALTKN', 'sId', 'stst_LegalName', 'stst_TradeName', 'stst_StreetAddress'

解决方案

var propNames = foo1.GetType()
                    .GetProperties()
                    .Select(pi => pi.Name)

这篇关于C#中:如何返回一个对象的所有属性名称的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆