Reputation: 368
I am using an attribute which should only be attached to properties with default accessors, and I am trying to enforce this coding convention programmatically.
This should be valid:
[Landing]
public int N {get; set;}
This should be invalid:
[Landing]
public int N {get {return n;} set{n=value+1;}}
How can I check that the code is valid, even if only at runtime?
Upvotes: 2
Views: 195
Reputation: 36689
I don't think you can (without outrageous hacking). The whole point of properties is to hide the getter and setter implementation, so it is hidden.
Why the latter example is invalid? Maybe you could handle it as well? If not, have you considered using public fields instead of properties? These will always be standard.
Upvotes: 2
Reputation: 4164
For an auto-property, the compiler just generates the backing field for you, so there's nothing special about an auto-property that you could discover at runtime.
You could try setting then getting the value to see if it's the same, but that's not going to prove that there's no extra code in the implementation.
For example, would you be okay if the setter fired an event before storing the value? There are lots of perfectly valid things a property implementation could do...
Upvotes: 1