Hook Event for All Property Set?

  • Thread starter Thread starter lucius
  • Start date Start date
L

lucius

Can I have an example of wiring up an Event (handler) that can be
fired no matter what property Set accessor is touched? I have a lot of
get/sets on public properties and I would like to create something in
the class constructor that would be automatically called whenever
anything was Set.

Thanks.
 
Hi Lucius,

Regarding on the central property setting event hooking request, based on
my understanding, so far we have to manually add some code to monitor all
the properties's get/set operations. This is because there is no built-in
pipeline or channel chain (for property setting/getting) for normal .NET
framework classes. If you do want to make your classes have such ability,
you can consider the following approaches:

** Add a event handler for your class that will react on propery get or
set. And in each of your class property's getter or setter, manually add
code to raise that event (and invoke the eventhandler).

** Or you can always use a central method or property to get/set other
properties(using reflection code in it). Thus, you can put your event
hooking code in that central place:

http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=646

BTW, there is another AOP like approach for you to define a proxy class
which will router any method calls to the backend real class instance.
However, using this maybe a bit to expensive for your scenario here. Just
mention it for your refererence:

#Aspect Oriented Programming using .NET - AOP in C#
http://www.developerfusion.co.uk/show/5307/3/

http://www.ayende.com/Blog/archive/2007/07/02/7-Approaches-for-AOP-in-.Net.a
spx

Hope this helps some.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.
 
Can I have an example of wiring up an Event (handler) that can be
fired no matter what property Set accessor is touched? I have a lot of
get/sets on public properties and I would like to create something in
the class constructor that would be automatically called whenever
anything was Set.

Thanks.


I don't think (never say never...) it can be done "automagically" -
instead, you will have to add some code into each setter to raise the
event. BTW, the INotifyPropertyChanged interface exists for this
purpose. There's an example in the help.
 
Hi lucius,

for the first approach, as Philip has also mentioned, here is a simple
implementation:

======================================
public delegate void PropertyChangeEventHandler(string propname, object
propvalue);

public class EventClass
{
private string _prop1;
private int _prop2;

public event PropertyChangeEventHandler PropertyChanged;

public EventClass()
{

}

public string Property1
{
get { return _prop1; }
set {
_prop1 = value;
if (PropertyChanged)
{
PropertyChanged("Property1", value);
}
}
}

public int Property2
{
get { return _prop2; }
set {
_prop2 = value;
if (PropertyChanged)
{
PropertyChanged("Property2", value);
}
}
}

}
=======================================

For the other global property approach, the article I refered has well
demonstrated the usage of global property(through reflection).

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Lucius,

Does the further info also help some? If there is anything else need help,
please don't hesitate to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top