In both Ruby and PHP (and I guess other languages as well) there are some utility methods that are called whenever a property is set. ( *instance_variable_set* for Ruby, *__set* for PHP).
So, let's say I have a C# class like this:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
Now, let's say that if any property setter from the Person
class is called, I want to call another method first, and then continue with the default behaviour of the setter, and the same applies for the property setters.
Is this possible?
Edit: I want to do this without defining a backing field.
You will have to write the properties in full to achieve this.
Yes, you may use the Decorator Design Pattern.
Yes, of course...
In your example you are using automatic properties, without a backing field.... You just need to create a backing field for your property, and then you can do what you want in the setter and getter.
example:
I know this has been properly answered but I'll include an example to show you the syntax to achieve what you want:
Mocking frameworks can do this, as well as IoC libraries like Unity. The only other way to do such a thing would be to use IL-rewriting (as previously mentioned).
You cant use automatic properties. You would have to dinfe the property out the old fashion way with a backing field and call the method manually.