C# – Notify modified properties of class

Today I follow a thread on mycsharp.de and discover an interesting post about finding out which property is just changed or modified http://www.mycsharp.de/wbb2/thread.php?threadid=80535 . This post was written in German and I just want to translate it into English. This example illustrates how we can monitor changes of properties. In the example below, I make a rectangle class with two properties Width, Height and Area property which is calculated by multiple Width with Height.

public class CustomRectangle
{
	private int nWidth;
	private int nHeight;

	public int Height
	{
		get { return nHeight; }
		set { nHeight = value; }
	}

	public int Width
	{
		get { return nWidth; }
		set { nWidth = value; }
	}

	public int Area
	{
		get { return Height * Width; }
	}
}

To notify which property is changed, the CustomRectangle class should implement INotifyPropertyChanged. Therefore I made some modifications to raise an event when a property is changed.

public class CustomRectangle : INotifyPropertyChanged
{
	...

	public int Height
	{
		...
		set { nHeight = value; NotifyPropertyChanged(MethodInfo.GetCurrentMethod()); }
	}

	public int Width
	{
		...
		set { nWidth = value; NotifyPropertyChanged(MethodInfo.GetCurrentMethod()); }
	}
	...
	public event PropertyChangedEventHandler PropertyChanged;
	public void NotifyPropertyChanged(String propertyName)
	{
		if (PropertyChanged != null)
		{
			PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
		}	   
		...
	}    
        public void NotifyPropertyChanged(System.Reflection.MethodBase methodBase)
	{
		NotifyPropertyChanged(methodBase.Name.Substring(4));
	} 
}

In the code snippet above, reflection was used to identify which method is being called. However the property Area based on Width and Height still does not raise any event. This property is changed when Width or Height is changed, therefore we must notify the property Area whenever Width/Height is set. As introduced in the thread on mycsharp.de, we can use a custom attribute for Area and then use reflection to find out which property Area depends on, is changed to raise an event simultaneously.

[DependsUpon("Height", "Width")]
public int Area
{
	get { return Height * Width; }
}

[global::System.AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
sealed class DependsUponAttribute : Attribute
{
	#region Private Members
	private readonly string[] lstProperties;
	#endregion

	#region Public Properties
	public string[] Properties
	{
		get { return lstProperties; }
	}
	#endregion

	#region Constructors
	public DependsUponAttribute(params string[] properties)
	{
		this.lstProperties = properties;
	}
	#endregion
}

public void NotifyPropertyChanged(String propertyName)
{
	if (PropertyChanged != null)
	{
		PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
	}   
	if (dictPropertyDependencies.ContainsKey(propertyName))
	{
		string[] depended = dictPropertyDependencies[propertyName];
		foreach (var propName in depended) NotifyPropertyChanged(propName);
	}  
}

The main idea of this method is introduced above however you will need the complete source code to understand more about this structure. You can download here “Notify modified properties