Reflection : GetEvent, GetMethod, Bind Method on Event

C

Claude

TValue oItem = (TValue)Activator.CreateInstance(typeof(TValue),
BindingFlags.Instance | BindingFlags.NonPublic,
null,
new object[] { m_nCleTemporaire },
System.Globalization.CultureInfo.CurrentCulture);

EventInfo oPropertyChanged = oItem.GetType().GetEvent("PropertyChanged");
MethodInfo eventHandler = this.GetType().GetMethod("Item_PropertyChanged",
BindingFlags.Instance | BindingFlags.NonPublic);

/** Next code line fails **/
Delegate d = Delegate.CreateDelegate(oPropertyChanged.EventHandlerType,
eventHandler);

The system says that it cannot murge the delegate with the defined type.

Why ???
Claude
 
I

Ignacio Machin ( .NET/ C# MVP )

TValue oItem = (TValue)Activator.CreateInstance(typeof(TValue),
                         BindingFlags.Instance |BindingFlags.NonPublic,
                         null,
        new object[] { m_nCleTemporaire },                                 
                        System.Globalization.CultureInfo.CurrentCulture);

EventInfo oPropertyChanged = oItem.GetType().GetEvent("PropertyChanged");
MethodInfo eventHandler = this.GetType().GetMethod("Item_PropertyChanged",  
BindingFlags.Instance | BindingFlags.NonPublic);

/** Next code line fails **/
Delegate d = Delegate.CreateDelegate(oPropertyChanged.EventHandlerType,
eventHandler);

The system says that it cannot murge the delegate with the defined type.

Why ???
 Claude

Are you sure that PropertyChanged event is of type EventHandlerType?
Most usually you get this error because of that
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top