我的代码返回一个空白的 PropertyInfo 数组
PropertyInfo[] classProperties = typeof(Processor).GetProperties();
此类中的所有属性都是公共(public)的。 使用.NET 2.0框架。
我还尝试使用之前在代码中声明的实例:
PropertyInfo[] classProperties = Computer.Processor[0].GetType().GetProperties();
我尝试过使用默认、实例和公共(public)等绑定(bind)。
有什么想法吗?
最佳答案
无参数形式将返回公共(public)属性。所以有两种可能的选择:
- 它们不是属性(而是字段)
- 它们不公开
公共(public)属性是带有 public
修饰符的 a: 和带有 get
或 set
访问器的 b:,例如:
public int Foo {get;set;} // automatically implemented property
public string bar;
public string Bar { // manually implemented property
get { return bar; }
set { bar = value; }
}
另请注意,仅当您查询接口(interface)而不是类时,才会反射(reflect)作为显式接口(interface)实现实现的接口(interface)绑定(bind)属性;因此,除非您从 typeof(ISomeInterface)
开始,否则不会显示以下内容:
string ISomeInterface.Bar { get { return someValue; } }
关于C# 反射有帮助吗? (获取属性),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9733416/