Is it possible to intercept an attempt to access of a property before it happens?

M

martin

just wondering....

Say i have a class with 3 nonstatic properties P1, P2 and P3. Now
somebody creates an instance of my class and calls any of them
(instance1.P2)
Is it possible to execute some other function say (checkIfLoaded)
before the property accessor is executed?
I realize i could do achieve this simply by embedding the call to
checkIfLoaded in the get{} accessor of each property
but i like to make things complicated. Any ideas?
 
J

Jon Skeet [C# MVP]

martin said:
just wondering....

Say i have a class with 3 nonstatic properties P1, P2 and P3. Now
somebody creates an instance of my class and calls any of them
(instance1.P2)
Is it possible to execute some other function say (checkIfLoaded)
before the property accessor is executed?
I realize i could do achieve this simply by embedding the call to
checkIfLoaded in the get{} accessor of each property
but i like to make things complicated. Any ideas?

You could add an event, subscribe to it from elsewhere, and then make
your property raise the event before returning the value.
 
M

martin

Interesting... thanks to the both of you for your responses.
The event idea would of course work but it would involve modifying the
get accessor - which is what i wanted to avoid in the first place.
I took a quick look at the Spring.NET framework you suggested. Looks
pretty good. I'll have to read up on it and see how to use it. Thanks
again
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top