NotifyPropertyChanged in .Net 4

I have code like this

private decimal m_Amount;
public decimal Amount
{
    get
    {
         return m_Amount;
    }
    set
    {
         m_Amount = value;
         NotifyPropertyChanged("Amount");
    }
}

But I want to do the without declaring the m_Amount variable and Notify also in .Net 4.

In this scenario how do avoid the backup field.

Please suggest me.

Jon Skeet
people
quotationmark

Well, if you use a C# 5 compiler (VS2012/VS2013) and the Microsoft.Bcl NuGet package, you can use CallerMemberNameAttribute with .NET 4. You'll still need to declare the field separately though, because you can't just use an automatically-implemented property. (You're doing more than just a simple read/write.)

Alternatively, you could use an AOP package like PostSharp - I wouldn't start using PostSharp just to get away from writing properties like this, but if you have other cross-cutting concerns you want to handle declaratively, it would be useful.

If you don't want to use the BCL NuGet package, you can just declare CallerMemberNameAttribute yourself - just copy and paste the declaration into your own code, but making sure you put it into the right namespace (System.Runtime.CompilerServices). Note that this will cause issues when you later update to .NET 4.5.

people

See more on this question at Stackoverflow