Click here to Skip to main content
15,867,986 members
Home / Discussions / C#
   

C#

 
GeneralRe: C# Application updated database. Pin
Ibrahim.elh28-Jan-16 3:19
Ibrahim.elh28-Jan-16 3:19 
GeneralRe: C# Application updated database. Pin
Ibrahim.elh28-Jan-16 4:15
Ibrahim.elh28-Jan-16 4:15 
GeneralRe: C# Application updated database. Pin
Richard Deeming28-Jan-16 4:21
mveRichard Deeming28-Jan-16 4:21 
GeneralRe: C# Application updated database. Pin
Ibrahim.elh28-Jan-16 4:34
Ibrahim.elh28-Jan-16 4:34 
GeneralRe: C# Application updated database. Pin
Ibrahim.elh28-Jan-16 5:41
Ibrahim.elh28-Jan-16 5:41 
Questionsetting multiple Locations in a bing Map Pin
yemen_mansour27-Jan-16 21:34
yemen_mansour27-Jan-16 21:34 
QuestionWindows 10 Mobile MDM - Device Provisioning Pin
gomes127-Jan-16 19:50
gomes127-Jan-16 19:50 
QuestionAdd WeakEvent to ObservableCollection using reflection Pin
Kenneth Haugland27-Jan-16 6:52
mvaKenneth Haugland27-Jan-16 6:52 
The is a continuation from the last question I had in implementing WeakEvents. I am able to add a handler to INotifyPropertyChange property using reflection, and also check if it exists within the class:
C#
MethodInfo addHandlerMethod = SendOrListenType.GetMethod("AddHandler");
if (typeof(INotifyPropertyChanged).IsAssignableFrom(_WeakBoundObject.Target.GetType()))
    addHandlerMethod.Invoke(null, new object[] { _WeakBoundObject.Target, "PropertyChanged", handler });

My problem arises when I wanted to add WeakHandlers to an ObservableCollection. I am able to find the event CollectionChanged:
C#
EventInfo er = _WeakBoundObject.Target.GetType().GetProperty(PropertyName).PropertyType.GetEvent("CollectionChanged");

And Im also able to check if it exists:
C#
if (typeof(INotifyCollectionChanged).IsAssignableFrom(_WeakBoundObject.Target.GetType().GetProperty(PropertyName).PropertyType))

but this refuses to compile:
C#
addHandlerMethod.Invoke(null, new object[] {_WeakBoundObject.Target.GetType().GetProperty(PropertyName).PropertyType, "CollectionChanged", handler });

I have tried loads of different alternatives, but I can find the right one, can anyone spot my mistake here?

[Edit]
I have also changed the code to initialize the WeakEventMAnager:
C#
Type unboundWEMType = typeof(WeakEventManager<,>);
Type[] typeArgs = { typeof(ObservableCollection<string>) , typeof(EventArgs) };
Type TempSendOrListenType = unboundWEMType.MakeGenericType(typeArgs);
MethodInfo addHandlerMethod2 = TempSendOrListenType.GetMethod("AddHandler");

if (typeof(INotifyCollectionChanged).IsAssignableFrom(_WeakBoundObject.Target.GetType().GetProperty(PropertyName).PropertyType))
    addHandlerMethod2.Invoke(null, new object[] {_WeakBoundObject.Target.GetType().GetProperty(PropertyName), "CollectionChanged", handler });


Stack trace:
C#
An exception of type 'System.ArgumentException' occurred in mscorlib.dll but was not handled in user code

Additional information: Object of type 'System.Reflection.RuntimePropertyInfo' cannot be converted to type 'System.Collections.ObjectModel.ObservableCollection`1[System.String]'.


modified 27-Jan-16 13:12pm.

AnswerRe: Add WeakEvent to ObservableCollection using reflection Pin
Richard Deeming27-Jan-16 7:17
mveRichard Deeming27-Jan-16 7:17 
GeneralRe: Add WeakEvent to ObservableCollection using reflection Pin
Kenneth Haugland27-Jan-16 7:21
mvaKenneth Haugland27-Jan-16 7:21 
GeneralRe: Add WeakEvent to ObservableCollection using reflection Pin
Mycroft Holmes27-Jan-16 12:05
professionalMycroft Holmes27-Jan-16 12:05 
GeneralRe: Add WeakEvent to ObservableCollection using reflection Pin
Kenneth Haugland27-Jan-16 22:09
mvaKenneth Haugland27-Jan-16 22:09 
AnswerRe: Add WeakEvent to ObservableCollection using reflection Pin
BillWoodruff27-Jan-16 14:52
professionalBillWoodruff27-Jan-16 14:52 
GeneralRe: Add WeakEvent to ObservableCollection using reflection Pin
Kenneth Haugland27-Jan-16 22:13
mvaKenneth Haugland27-Jan-16 22:13 
QuestionProgrammatically Attach Debugger Pin
Kevin Marois26-Jan-16 7:49
professionalKevin Marois26-Jan-16 7:49 
AnswerRe: Programmatically Attach Debugger Pin
OriginalGriff26-Jan-16 8:26
mveOriginalGriff26-Jan-16 8:26 
GeneralRe: Programmatically Attach Debugger Pin
Kevin Marois26-Jan-16 8:32
professionalKevin Marois26-Jan-16 8:32 
GeneralRe: Programmatically Attach Debugger Pin
OriginalGriff26-Jan-16 8:41
mveOriginalGriff26-Jan-16 8:41 
GeneralRe: Programmatically Attach Debugger Pin
Kevin Marois26-Jan-16 8:38
professionalKevin Marois26-Jan-16 8:38 
GeneralRe: Programmatically Attach Debugger Pin
OriginalGriff26-Jan-16 8:41
mveOriginalGriff26-Jan-16 8:41 
GeneralRe: Programmatically Attach Debugger Pin
Kevin Marois26-Jan-16 8:45
professionalKevin Marois26-Jan-16 8:45 
GeneralRe: Programmatically Attach Debugger Pin
OriginalGriff26-Jan-16 8:57
mveOriginalGriff26-Jan-16 8:57 
GeneralRe: Programmatically Attach Debugger Pin
Kevin Marois26-Jan-16 9:01
professionalKevin Marois26-Jan-16 9:01 
GeneralRe: Programmatically Attach Debugger Pin
OriginalGriff26-Jan-16 9:16
mveOriginalGriff26-Jan-16 9:16 
GeneralRe: Programmatically Attach Debugger Pin
Kevin Marois26-Jan-16 9:19
professionalKevin Marois26-Jan-16 9:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.