Is is possible to catch such event?
Hello,
I created class which has public property with get accesor.
This property returns other class.
Example:
Object obj = Myclass.MyObjectProperty
I need to know when this object is accesed with get and when with set accesor.
obj.ObjectsPropery = "I need to know, then SET accsor is fired"
or
string objectPropertyValue = obj.ObjectsPropery; This time GET accsor is fired.
Is any way to catch this trigers?
Event must be catched in Myclass class.
Re: Is is possible to catch such event?
Sure, just define PropertyGet and PropertySet events for your class. You could use the EventHandler delegate type, or define your own.
e.g.
Code:
delegate void PropertyEventHandler (Athing Sender);
class Athing
{
public event PropertyEventHandler GetAccess;
public event PropertyEventHandler SetAccess;
private long _someValue;
public long SomeValue
{
get
{
GetAccess(this);
return _someValue;
}
set
{
_someValue = value;
SetAccess(this);
}
}
}
But you do have to raise them in each accessor method. You may also want to define seperate events for each property.
Re: Is is possible to catch such event?
oooooooooooh big problem with your code, you need to check for null values (me thinks:D)
so
if (GetAccess!=null) GetAccess(this);
Re: Is is possible to catch such event?
My bad, yes you do need to do that, in case any inconsiderate fools don't allocate event handlers before assigning/reading property values :)