A
Andrus
I tried to attach Event handler to dynamically created
ObservableCollection but got ArgumentException in Delegate.CreateDelegate()
shown in comment.
How to fix ?
Andrus.
public event EventHandler ModifiedsChanged;
public IList Modifieds;
void AddEvent( Type objectType ) {
Type listType = typeof(ObservableCollection<>).MakeGenericType(new[] {
objectType });
Modifieds = (IList)Activator.CreateInstance(listType);
EventInfo eInfo = listType.GetEvent("CollectionChanged");
var mi = GetType().GetMethod("NotifyCollectionChanged",
BindingFlags.NonPublic | BindingFlags.Instance);
// ArgumentException: Error binding to target method.
var del = Delegate.CreateDelegate(eInfo.EventHandlerType, mi);
eInfo.AddEventHandler(Modifieds, del);
}
void NotifyCollectionChanged(object sender, NotifyCollectionChangedEventArgs
e) {
if (ModifiedsChanged != null)
ModifiedsChanged(this, EventArgs.Empty);
}
ObservableCollection but got ArgumentException in Delegate.CreateDelegate()
shown in comment.
How to fix ?
Andrus.
public event EventHandler ModifiedsChanged;
public IList Modifieds;
void AddEvent( Type objectType ) {
Type listType = typeof(ObservableCollection<>).MakeGenericType(new[] {
objectType });
Modifieds = (IList)Activator.CreateInstance(listType);
EventInfo eInfo = listType.GetEvent("CollectionChanged");
var mi = GetType().GetMethod("NotifyCollectionChanged",
BindingFlags.NonPublic | BindingFlags.Instance);
// ArgumentException: Error binding to target method.
var del = Delegate.CreateDelegate(eInfo.EventHandlerType, mi);
eInfo.AddEventHandler(Modifieds, del);
}
void NotifyCollectionChanged(object sender, NotifyCollectionChangedEventArgs
e) {
if (ModifiedsChanged != null)
ModifiedsChanged(this, EventArgs.Empty);
}