Chris Watts
Chris Watts

Reputation: 6725

C# Reflection Help? (GetProperties)

My code is returning a blank array of PropertyInfo

PropertyInfo[] classProperties = typeof(Processor).GetProperties();

All properties in this class are public. Using .NET 2.0 Framework.

I have also tried using an instance declared earlier in my code:

PropertyInfo[] classProperties = Computer.Processor[0].GetType().GetProperties();

And I have tried using bindings such as Default, Instance and Public.

Any ideas?

Upvotes: 2

Views: 357

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1064204

The parameterless form will return public properties. So there are 2 likely options:

  • they are not properties (but instead, fields)
  • they are not public

A public property is something a: with the public modifier, and b: with a get or set accessor, for example either of:

public int Foo {get;set;} // automatically implemented property
public string bar;
public string Bar { // manually implemented property
    get { return bar; }
    set { bar = value; }
}

Note also that interface-bound properties that are implemented as explicit interface implementation will only be reflected if you query against the interface, not the class; so the following will not show unless you start from typeof(ISomeInterface):

string ISomeInterface.Bar { get { return someValue; } } 

Upvotes: 5

Related Questions