1

I was wondering if I can use automatic properties and still be able to fire events on property changed. Here are my current classes. (The actual User class got way more properties/fields of course).

public delegate void UserEventHandler(object sender, EventArgs e);

public class User
{
    public event UserEventHandler Changed;

    private string _UserName;
    public string UserName
    {
        get
        {
            return _UserName;
        }
        private set
        {
            _UserName = value;
            this.OnChanged(EventArgs.Empty);
        }
    }

    protected void OnChanged(EventArgs e)
    {
        if (Changed != null)
        {
            Changed(this, e);
        }
    }
}

So I was wondering if there is a way I could take advantage of the automatic properties and still be able to fire the OnChanged events. In other words : Are semi-automatic properties possible?

Tipx
  • 7,367
  • 4
  • 37
  • 59
  • 3
    Why are you re-creating the built-in `EventHandler` delegate? Also, why don't you implement `INotifyPropertyChanged`? – SLaks Nov 04 '10 at 15:12
  • I re-created the EventHandler because I've been told I should use strong typed eventArgs. As for the "INotifyPropertyChanged", I was not aware of this interface, but it doesn't really help me in any ways because I still wouldn't be able to use the automatic properties. – Tipx Nov 04 '10 at 15:31
  • Your `UserEventHandler` is _identical_ to the built-in `EventHandler` delegate. If you have a strongly-typed `EventArgs` class, yuo should use the built-in `EventHandler` delegate. Under no circumstances should you create your own event handler delegate in .Net 2.0+. – SLaks Nov 04 '10 at 15:36
  • So why did MSFT create "PropertyChangedEventHandler" instead of EventHandler? I'll still implement the interface you suggested since it will be clearer for anyone else that looks at my code though. – Tipx Nov 04 '10 at 15:54
  • @Tipx: That, and every other XXXEventHandler delegate, was created in .Net 1.0 or 1.1, before generics. – SLaks Nov 04 '10 at 16:45
  • k, k. I checked on "http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=VS.90).aspx"and I couldn't see a version before 2.0 so I taught it was done in 2.0. Thanks. – Tipx Nov 04 '10 at 18:14
  • Please visit http://stackoverflow.com/questions/1425310/auto-implemented-properties-in-c-sharp/19474530#19474530 – Ali Shams Oct 20 '13 at 08:11

3 Answers3

5

You can use PostSharp.
Example

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • On one WPF project I worked on, we used PostSharp for this very purpose. We just slapped an attribute on all of our Auto Properties in our Models that needed to raise change notification, and it was awesome. Much better than writing all that boiler plate code. Compilation did get noticeably slower though. (but that's also partially because of the crappy boxes you get when working in Corporate America :-P ) – BFree Nov 04 '10 at 15:21
  • Thanks. I really like what I saw in the example. Sadly, with my role in the organisation I work at, I can't have them buy this kind of software. – Tipx Nov 04 '10 at 15:37
1

Very late to the party, but this question still appears on google.

There's a package which works in much the same way as the PostSharp example, but is free: Fody.Propertychanged.

The project's README, and the wiki pages it links to, do a very good job of explaining it.

canton7
  • 37,633
  • 3
  • 64
  • 77
0

I modified your code a little for the event can be accessed and using the ready made EventHandler.

    public class User
    {
        public event EventHandler AgeChanged;     
        private string _UserName;
        public string UserName
        {
            get
            {
                return _UserName;
            }
             set  
            {
                _UserName = value;
                this.OnAgeChanged(this,EventArgs.Empty);
            }
        }

        protected virtual void OnAgeChanged(object sender, EventArgs e)
        {
            if (AgeChanged != null)
            {
                AgeChanged(sender, e);
            }
        }
   }

How to Set Events:

      var user = new User();
     //subscribe to events
     user.AgeChanged+= (s,e) => Console.WriteLine("UserNamed changed to {0}",user.UserName);

    //modify UserName and now event is fired
     user.UserName="Jack";

see a working demo

M.Hassan
  • 10,282
  • 5
  • 65
  • 84