tags:

views:

31

answers:

3

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?

+2  A: 

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
+5  A: 

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
+2  A: 

You can use PostSharp to implement INotifyPropertyChanged interface dynamically.

Giorgi