Reflection on static objects

  • Thread starter Thread starter John Wood
  • Start date Start date
J

John Wood

The GetProperty ./ GetMethod etc. methods all require an instance as a first
parameter.

What if the property is static? I can't pass in a class, obviously...

John
 
John,

GetProperty and GetMethod do not require an instance as a first
parameter, they require a Type instance, and then you can get properties and
methods, regardless of whether or not they are static.

Invoking them is a different story, and in this case, if the
method/property is static, then the method/property info will know this, and
you can pass null as the parameter for the instance.

Hope this helps.
 
John Wood said:
The GetProperty ./ GetMethod etc. methods all require an instance as a first
parameter.

What if the property is static? I can't pass in a class, obviously...

Do you mean the PropertyInfo.GetValue methods etc? If so, pass in null
(as documented).
 
Sorry I meant GetValue on the MemberInfo those methods return...

And you're right, passing in null does it.

Thanks guys.

Nicholas Paldino said:
John,

GetProperty and GetMethod do not require an instance as a first
parameter, they require a Type instance, and then you can get properties and
methods, regardless of whether or not they are static.

Invoking them is a different story, and in this case, if the
method/property is static, then the method/property info will know this, and
you can pass null as the parameter for the instance.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

John Wood said:
The GetProperty ./ GetMethod etc. methods all require an instance as a first
parameter.

What if the property is static? I can't pass in a class, obviously...

John
 
Back
Top