如何获取类的属性列表?

时间:2009-04-10 09:29:55

标签: c# .net reflection properties

如何获取班级所有属性的列表?

10 个答案:

答案 0 :(得分:708)

反射;例如:

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)(返回所有公共/私有实例属性)。

答案 1 :(得分:75)

您可以使用反射执行此操作: (从我的图书馆 - 这得到名称和价值)

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[]{});
}

此外,仅获取公共属性:(see MSDN on BindingFlags enum

/* 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

但是我觉得这有点慢。

答案 2 :(得分:34)

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;
}

此功能用于获取类属性列表。

答案 3 :(得分:21)

您可以将System.Reflection命名空间与Type.GetProperties() mehod:

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

答案 4 :(得分:18)

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

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

答案 5 :(得分:6)

您可以使用反射。

Type typeOfMyObject = myObject.GetType();
PropertyInfo[] properties =typeOfMyObject.GetProperties();

答案 6 :(得分:3)

我也面临着这种要求。

从这次讨论中我得到了另一个想法,

Obj.GetType().GetProperties()[0].Name

这也显示了属性名称。

Obj.GetType().GetProperties().Count();

显示了多少属性。

感谢所有人。这是很好的讨论。

答案 7 :(得分: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();
 }

答案 8 :(得分:3)

尝试一下:

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

答案 9 :(得分:1)

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

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