C#反射帮助?(GetProperties)
本文关键字:GetProperties 反射 帮助 | 更新日期: 2023-09-27 18:27:55
我的代码返回PropertyInfo 的空白数组
PropertyInfo[] classProperties = typeof(Processor).GetProperties();
此类中的所有属性都是公共的。使用.NET 2.0 Framework。
我还尝试使用代码中早些时候声明的实例:
PropertyInfo[] classProperties = Computer.Processor[0].GetType().GetProperties();
我还尝试过使用诸如Default、Instance和Public之类的绑定。
有什么想法吗?
无参数表单将返回公共属性。因此有两种可能的选择:
- 它们不是属性(而是字段)
- 它们不是公开的
公共属性是A:带有public
修饰符,b:带有get
或set
访问器,例如:
public int Foo {get;set;} // automatically implemented property
public string bar;
public string Bar { // manually implemented property
get { return bar; }
set { bar = value; }
}
还要注意,作为显式接口实现实现的接口绑定属性只有在查询接口而不是类时才会反映出来;因此,除非您从typeof(ISomeInterface)
开始,否则以下内容将不会显示:
string ISomeInterface.Bar { get { return someValue; } }