I wrote my own idea of your requirements, but I'm not sure if it fits your needs. The changes to INotifyProperty are something you could also explore, but I don't like it very much, because it looks like speghetti wiring. Perhaps this will give you some creative ideas.
What this means is that it allows you to use an ObservableObject as for all your property types. By doing this, each property will have an ObjectChanged event with which you can connect. Con is that you must initialize all your properties in the constructor to throw a NullReferenceException in your code.
This example uses three classes.
- ObservableObject.cs
- Employee.cs
- Program.cs
ObservableObject.cs
//----------------------------------------------------------------------------- // <copyright file="ObservableObject.cs" company="DCOM Productions"> // Copyright (c) DCOM Productions. All rights reserved. // </copyright> //----------------------------------------------------------------------------- namespace PropertyChangedEventExample { using System; public class ObservableObject : Object { /// <summary> /// Expose the default constructor /// </summary> public ObservableObject() { // No default implementation } private object m_Object = null; /// <summary> /// Base object /// </summary> public object Object { get { return m_Object; } set { if (m_Object != value) { m_Object = value; OnObjectChanged(this, EventArgs.Empty); } } } /// <summary> /// Triggered when the value of this object has changed. /// </summary> public event System.EventHandler<EventArgs> ObjectChanged; /// <summary> /// EventHandler wire-up /// </summary> protected virtual void OnObjectChanged(object sender, System.EventArgs e) { if (ObjectChanged != null) { ObjectChanged(sender, e); } } /// <summary> /// Gets the value /// </summary> public object Get() { return this.Object; } /// <summary> /// Sets the value /// </summary> public void Set(object value) { this.Object = value; } } }
Employee.cs
//----------------------------------------------------------------------------- // <copyright file="Employee.cs" company="DCOM Productions"> // Copyright (c) DCOM Productions. All rights reserved. // </copyright> //----------------------------------------------------------------------------- namespace PropertyChangedEventExample { using System; public class Employee { /// <summary> /// Expose default constructor /// </summary> public Employee() { Name = new ObservableObject(); } /// <summary> /// Gets or sets the name /// </summary> public ObservableObject Name { get; set; } } }
Program.cs
//----------------------------------------------------------------------------- // <copyright file="Program.cs" company="DCOM Productions"> // Copyright (c) DCOM Productions. All rights reserved. // </copyright> //----------------------------------------------------------------------------- namespace PropertyChangedEventExample { using System; class Program { static void Main(string[] args) { Employee employee = new Employee(); employee.Name.Set("David"); employee.Name.ObjectChanged += new EventHandler<EventArgs>(Name_ObjectChanged); employee.Name.Set("Dave"); Console.ReadKey(true); } static void Name_ObjectChanged(object sender, EventArgs e) { ObservableObject employee = sender as ObservableObject; Console.WriteLine("Name changed to {0}", employee.Get()); } } }
David Anderson - DCOM
source share