Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Expression.PropertyOrField be used to access a static property or field?

The documentation of the expression parameter of Expression.PropertyOrField states that it can be null for static members:

An Expression whose Type contains a property or field named propertyOrFieldName . This can be null for static members.
(Emphasis mine)

However, whenever I pass null I get an ArgumentNullException.

like image 550
Daniel Hilgarth Avatar asked Mar 23 '23 03:03

Daniel Hilgarth


1 Answers

The documentation of this method is contradictory:

  • The documentation of the expression parameter states that it can be null for static members
  • The documentation of the ArgumentNullException states that it is thrown if expression is null

Fact is:
This method can't be used to get access to static members, the documentation of the expression parameter is incorrect.
Even when an expression with the correct Type is supplied, this method doesn't work as expected, because it only ever looks for instance members.

To access a static field or property, use Expression.MakeMemberAccess instead.

like image 67
Daniel Hilgarth Avatar answered Apr 06 '23 09:04

Daniel Hilgarth