0

I have some code I would like to simplify. Here is the code:

private int id;
public int Id
{
    get { return id; }
    set
    {
        id = value;
        base.OnPropertyChanged("Id");
    }
}

I was thinking using an Attribute to only get:

[RaiseOnPropertyChanged()]
public int Id { get; set; }

How can that be done? Or can it be done using an attribute?

joerage
  • 4,863
  • 4
  • 38
  • 48
  • see http://stackoverflow.com/questions/488587/whats-the-best-way-to-call-inotifypropertychangeds-propertychanged-event – Stan R. Dec 29 '09 at 19:30

3 Answers3

5

Alas, you can't do that within the confines of C# itself; you'll need to manipulate things at the IL level to get what you want. However, you can use aspect-oriented techniques like that provided by PostSharp to get the desired result.

John Feminella
  • 303,634
  • 46
  • 339
  • 357
2

The best option for you to explore is Aspect-Oriented-Programming using a framework such as PostSharp.

PostSharp will let you define attributes that will be 'fleshed-out' with code templates during compile time. Their site has several examples and their upcoming major release will significantly ease working with the tool for exactly these types of scenarios.

STW
  • 44,917
  • 17
  • 105
  • 161
2

You can use PostSharp to implement INotifyPropertyChanged interface dynamically.

Giorgi
  • 30,270
  • 13
  • 89
  • 125