Is there a way in C# to have a setter inherit from “something” that would allow me to run some code each time a setter is invoked for a particular base class and it’s inheritors?
What I want to do is have a boolean flag on my base class called IsValid. I will set this boolean to true after validating an object, then if any setter is invoked after validation, I want the setter to change the IsValid flag to false.
I don’t want to have to write this logic into every setter.
You are basically asking for some version of Aspect Oriented Programming (AOP). You have a common operation that you want to perform across all properties, without implementing it in every case.
I’m not sure what the choices really are for AOP in C#, but one I have used before is Spring.Net AOP. What it will do is wrap your class in a dynamically created wrapper which will intercept all calls to your properties and then allow you to implement some common logic to execute.