My guess you're getting the exception because nobody has subscribed to the
event. As I said before you're invoking the handler and not invoking the
event. In "normal" C# you'd write something like this:
Well officially, the control has subscribed, but I understand that what I
was doing did not stand much chance of working.
So, I resorted to using an internal Dictionary<string, EventHandler> like
this
/////////////////////////////
public class EventTest
{
private Dictionary<string, object> values = new Dictionary<string,
object>();
protected Dictionary<string, EventHandler> events = new
Dictionary<string, EventHandler>();
public EventTest()
{
PropertyInfo[] properties = GetType().GetProperties();
foreach(PropertyInfo propInfo in properties)
{
Type[] paramTypes = new Type[] { propInfo.PropertyType };
Type testType =
typeof(TestType<>).BindGenericParameters(paramTypes);
valueTypes.Add(propInfo.Name, Activator.CreateInstance(testType));
}
EventInfo[] events = GetType().GetEvents();
foreach(EventInfo ei in events)
this.events.Add(ei.Name, null);
}
private const string sChanged = "Changed";
private void AddChangedDelegate(string name, EventHandler del)
{
events[name + sChanged] += del;
}
private void RemoveChangedDelegate(string name, EventHandler del)
{
events[name + sChanged] -= del;
}
private void OnChanged(string name, EventArgs args)
{
if(events[name] != null)
events[name](this, args);
}
protected object GetValue(string name)
{
return values[name]);
}
protected void SetValue(string name, object value)
{
values[name]) = value;
OnChanged(name, EventArgs.Empty);
}
private const string sName = "Name";
private const string sAge = "Age";
public string Name
{
get { return GetValue(sName) == null ? String.Empty : (string)
GetValue(sName); }
set { SetValue(sName, value); }
}
public int Age
{
get { return (int) GetValue(sAge); }
set { SetValue(sAge, value); }
}
public event EventHandler NameChanged
{
add { AddChangedDelegate(sName, value); }
remove { RemoveChangedDelegate(sName, value); }
}
}
/////////////////////
This is much simpler, only requiring the obligatory (PropertyName)Changed
events for each property

+ :-(
Although I am not jettisoning my code as it makes handling events not
destined for controls easier to manage.
But wait, I might have something for you! If I understand correctly you're
binding your own classes to controls and you don't want to add all
corresponding <property name>Changed events. If that's the case then have a
look at this excellent article:
http://www.interact-sw.co.uk/iangblog/2004/10/23/propertychange
Now why didn't you post that earlier !!! <vbg>
I had just finished the version above, checked the newsgroups, read this
article, picked my jaw up from the desk

, changed my code and all is well
with the world !!!
Thank you, thank you, thank you.
I might have been doing OO design for years, but this .NET framework is just
so massive; thank goodness for the internet allowing us to 'meld minds'.
Joanna