Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / July 2007

Tip: Looking for answers? Try searching our database.

Hook Event for All Property Set?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
lucius - 09 Jul 2007 15:32 GMT
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.
Steven Cheng[MSFT] - 10 Jul 2007 07:05 GMT
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.
lucius - 10 Jul 2007 16:57 GMT
Yes, you understand my issue. Can you please code a sample that does

"
>** 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).
"
Please?

Thanks.

>Hi Lucius,
>
[quoted text clipped - 56 lines]
>
>This posting is provided "AS IS" with no warranties, and confers no rights.
Steven Cheng[MSFT] - 11 Jul 2007 05:08 GMT
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.
Steven Cheng[MSFT] - 13 Jul 2007 08:13 GMT
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.
PhilipDaniels@foo.com - 10 Jul 2007 19:37 GMT
>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
[quoted text clipped - 3 lines]
>
>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.

--
Philip Daniels

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.