C#反射帮助?(的GetProperties)

CJx*_*JxD 2 c# reflection properties

我的代码返回一个PropertyInfo的空白数组

PropertyInfo[] classProperties = typeof(Processor).GetProperties();
Run Code Online (Sandbox Code Playgroud)

此类中的所有属性都是公共的.使用.NET 2.0 Framework.

我也尝试使用我的代码中先前声明的实例:

PropertyInfo[] classProperties = Computer.Processor[0].GetType().GetProperties();
Run Code Online (Sandbox Code Playgroud)

我尝试过使用Default,Instance和Public等绑定.

有任何想法吗?

Mar*_*ell 5

无参数表单将返回公共属性.所以有两种可能的选择:

  • 它们不是属性(而是字段)
  • 他们不公开

公共属性是:使用public修饰符,b:使用getset访问者,例如:

public int Foo {get;set;} // automatically implemented property
public string bar;
public string Bar { // manually implemented property
    get { return bar; }
    set { bar = value; }
}
Run Code Online (Sandbox Code Playgroud)

另请注意,实现为显式接口实现的接口绑定属性仅在您对接口而不是类进行查询时才会反映出来.所以除非你从以下开始,否则以下内容不会显示typeof(ISomeInterface):

string ISomeInterface.Bar { get { return someValue; } } 
Run Code Online (Sandbox Code Playgroud)