如何获取一个类的属性列表?

773

我怎样获取类的所有属性列表?


这个可能也会有帮助:这里 - Matt
11个回答

1023

反射; 举个例子:

obj.GetType().GetProperties();

对于一个类型:

typeof(Foo).GetProperties();
例如:
class Foo {
    public int A {get;set;}
    public string B {get;set;}
}
...
Foo foo = new Foo {A = 1, B = "abc"};
foreach(var prop in foo.GetType().GetProperties()) {
    Console.WriteLine("{0}={1}", prop.Name, prop.GetValue(foo, null));
}

根据反馈...

  • 若要获取静态属性的值,请将null作为第一个参数传递给GetValue
  • 若要查看非公共属性,请使用(例如)GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)(它返回所有公共/私有实例属性)。

16
为了完整起见,还有 ComponentModel,可以通过 TypeDescriptor.GetProperties(...) 来公开访问,它允许动态运行时属性(反射在编译时已固定)。 - Marc Gravell
6
建议:将答案扩展以涵盖受保护的/私有的/静态的/继承的属性。 - Richard
1
你展示的foreach语句甚至可以在你想获取属性的类内部工作 :) - halfpastfour.am
1
@Tadej 你打算使用哪个框架?如果你正在使用.NET Core,你需要确保有 using System.Reflection 指令和 System.Reflection.TypeExtensions 包的引用 - 这将通过扩展方法提供缺失的API表面。 - Marc Gravell
1
@HarryPehkonen,我认为你的例子不是属性,而是字段,所以你可以使用foo.GetType().GetFields()提取所有信息。 - Vic
显示剩余8条评论

122
你可以使用反射来实现这一点: (从我的库中获取名称和值)
public static Dictionary<string, object> DictionaryFromType(object atype)
{
    if (atype == null) return new Dictionary<string, object>();
    Type t = atype.GetType();
    PropertyInfo[] props = t.GetProperties();
    Dictionary<string, object> dict = new Dictionary<string, object>();
    foreach (PropertyInfo prp in props)
    {
        object value = prp.GetValue(atype, new object[]{});
        dict.Add(prp.Name, value);
    }
    return dict;
}

这个东西无法处理带有索引的属性 - 对于这种情况(它变得笨重):

public static Dictionary<string, object> DictionaryFromType(object atype, 
     Dictionary<string, object[]> indexers)
{
    /* replace GetValue() call above with: */
    object value = prp.GetValue(atype, ((indexers.ContainsKey(prp.Name)?indexers[prp.Name]:new string[]{});
}

此外,要仅获取公共属性:(参见 MSDN 上的 BindingFlags 枚举
/* replace */
PropertyInfo[] props = t.GetProperties();
/* with */
PropertyInfo[] props = t.GetProperties(BindingFlags.Public)

这也适用于匿名类型!
要仅获取名称:

public static string[] PropertiesFromType(object atype)
{
    if (atype == null) return new string[] {};
    Type t = atype.GetType();
    PropertyInfo[] props = t.GetProperties();
    List<string> propNames = new List<string>();
    foreach (PropertyInfo prp in props)
    {
        propNames.Add(prp.Name);
    }
    return propNames.ToArray();
}

对于值本身来说,也可以使用相同的方法:

GetDictionaryFromType().Keys
// or
GetDictionaryFromType().Values

但我想那会慢一些。


但是 atype.GetProperty(prp.Name) 会返回 prp 吗? - Marc Gravell
11
关于公共属性位,根据链接的MSDN文章:“注意 如果没有指定Instance或Static,同时也没有指定Public或NonPublic,则不会返回任何成员。”因此示例代码应该是:t.GetProperties(BindingFlags.Instance | BindingFlags.Public) 或者 t.GetProperties(BindingFlags.Static | BindingFlags.Public) - Carl Sharman
我不是在找代码,我是在寻找关于反射的解释,非常感谢!如果这个变得笼统,你可能只是在说你的程序有超能力 ;) - Jaquarh

44
public List<string> GetPropertiesNameOfClass(object pObject)
{
    List<string> propertyList = new List<string>();
    if (pObject != null)
    {
        foreach (var prop in pObject.GetType().GetProperties())
        {
            propertyList.Add(prop.Name);
        }
    }
    return propertyList;
}

这个函数用于获取类属性列表。


7
你可能需要改为使用 yield return。这不是什么大问题,但这是更好的做法。 - Matthew Haugen
1
我喜欢这个答案,因为它(几乎)是唯一一个不包含“反射”一词的答案。 - user4624979
9
但是这仍然使用了反射。 - GGG
2
我想这样会好很多:pObject.GetType().GetProperties().Select(p=>p.Name) - Disappointed

33

基于 @MarcGravell 的答案,这里是一个适用于 Unity C# 的版本。

ObjectsClass foo = this;
foreach(var prop in foo.GetType().GetProperties()) {
    Debug.Log("{0}={1}, " + prop.Name + ", " + prop.GetValue(foo, null));
}

25

您可以使用 System.Reflection 命名空间中的 Type.GetProperties() 方法:

PropertyInfo[] propertyInfos;
propertyInfos = typeof(MyClass).GetProperties(BindingFlags.Public|BindingFlags.Static);

15

以下代码将为您提供类属性/属性/表列的列表

var Properties = typeof(className).GetProperties().Select(x=>x.Name).ToList();

14

试一下这个:

var model = new MyObject();
foreach (var property in model.GetType().GetProperties())
{
    var descricao = property;
    var type = property.PropertyType.Name;
}

2
这个回答以最直接的方式回答了提问者所问的问题。6行搞定。 - Joe Johnston

10

那是我的解决方案

public class MyObject
{
    public string value1 { get; set; }
    public string value2 { get; set; }

    public PropertyInfo[] GetProperties()
    {
        try
        {
            return this.GetType().GetProperties();
        }
        catch (Exception ex)
        {

            throw ex;
        }
    }

    public PropertyInfo GetByParameterName(string ParameterName)
    {
        try
        {
            return this.GetType().GetProperties().FirstOrDefault(x => x.Name == ParameterName);
        }
        catch (Exception ex)
        {

            throw ex;
        }
    }

    public static MyObject SetValue(MyObject obj, string parameterName,object parameterValue)
    {
        try
        {
            obj.GetType().GetProperties().FirstOrDefault(x => x.Name == parameterName).SetValue(obj, parameterValue);
            return obj;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}

请不要这样做。这个S.O.应该尽力回答一个关于如何正确完成某件事情的问题。如果你的库需要,或者甚至允许使用成员名称的字符串表示来修改你的强类型对象,那么你的代码库就存在着巨大的问题。并不是说在某些场景下这不是必要的,比如你明显无法控制系统的其他部分、公司政治或其他事情。但这看起来像是一项架构决策,而且是一个可怕的决策。这是一系列可怕的编程选择中的一个决策。 - Suamere

7
你可以使用反射。
Type typeOfMyObject = myObject.GetType();
PropertyInfo[] properties =typeOfMyObject.GetProperties();

3

这里是改进后的 @lucasjones 答案。我包含了他回答后评论区提到的改进。希望这对某人有所帮助。

public static string[] GetTypePropertyNames(object classObject,  BindingFlags bindingFlags)
{
    if (classObject == null)
    {
        throw new ArgumentNullException(nameof(classObject));
    }

        var type = classObject.GetType();
        var propertyInfos = type.GetProperties(bindingFlags);

        return propertyInfos.Select(propertyInfo => propertyInfo.Name).ToArray();
 }

我猜你可以编辑他的答案,而不是自己写一个。基本上这样做会少看到一个给出相同观点的答案。 - Mantra

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接