jeudi 27 juin 2019

Detach Event Handler From Dynamic Object

I am trying to detach an event handler using a dynamic object. I haven't use dynamic much, and I'm not sure where I'm going wrong here. The exception I'm receiving is:

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException

'object' does not contain a definition for 'CollectionChanged'

Dictionary<string, object> _values;
Dictionary<string, NotifyCollectionChangedEventHandler> _collectionChangedDelegates;

void ClearDelegates()
{
    foreach (var kvp in _values)
    {
        var currentValue = _values[kvp.Key];
        if (currentValue == null)
            continue;

        var type = currentValue.GetType();

        if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(ObservableCollection<>))
        {
            dynamic observableCollection = currentValue;
            observableCollection.CollectionChanged -= _collectionChangedDelegates[kvp.Key];
        }
    }
}





Aucun commentaire:

Enregistrer un commentaire