C# Type.GetProperties方法

C# Type.GetProperties方法,c#,.net,reflection,C#,.net,Reflection,我有一门课是这样的: class ItemList { Int64 Count { get; set; } } 当我写这篇文章时: ItemList list = new ItemList ( ); Type type = list.GetType ( ); PropertyInfo [ ] props = type.GetProperties ( ); 我得到一个空的道具阵列 为什么??是因为GetProperties不包含自动属性吗?问题是GetProperties默认情况下只

我有一门课是这样的:

class ItemList
{
    Int64 Count { get; set; }
}
当我写这篇文章时:

ItemList list = new ItemList ( );

Type type = list.GetType ( );
PropertyInfo [ ] props = type.GetProperties ( );
我得到一个空的道具阵列


为什么??是因为GetProperties不包含自动属性吗?

问题是GetProperties默认情况下只返回公共属性。在C#中,默认情况下成员不是公共的(我相信他们是内部的)。试试这个

var props = type.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic);
BindingFlags枚举相当灵活。上述组合将返回该类型上的所有非公共实例属性。不过,您可能想要的是所有实例属性,而不考虑可访问性。在这种情况下,请尝试以下方法

var flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
var props = type.GetProperties(flags);

谢谢,我不知道。另外,如何为单个参数提供多个选项?你是不是在换位子?@琼,是的。BindingFlags是一个枚举,它使用可使用|操作的位标志。它没有提供多个参数,只是创建了一个设置了各种位组合的枚举值。琼:绑定标志是一个标志枚举,因此您可以使用|将多个标志传递到函数中。参考:
给所有来自谷歌的人:
如果您在设置标志后仍然无法获取“属性”,如果您是c#新手,不知道
属性
字段
之间的区别,请尝试使用
GetFields()
。您可能一直在寻找
字段