Finding a custom event using reflection
-
Hello Gurus. Yes, I've painted myself into yet another corner. It seems to be what I do best, so I guess I should stick with it. This time I am trying use reflection to find an event that I have attached to a class, but reflection is not showing the event. I don't know why. I have created a simple class called ListPicker that is based on the DomainUpDown control. It is supposed to act just like a DomainUpDown control, but it supports the Modified property and the ModifiedChanged events that DomainUpDown does not. Here is the code:
using System;
using System.Windows.Forms;namespace WindowsFormsControlLibrary
{
public partial class ListPicker : DomainUpDown
{
public EventHandler ModifiedChanged;public ListPicker() { InitializeComponent(); // Register our TextChanged hander. TextChanged += new EventHandler(ListPicker\_TextChanged); Modified = false; } public bool Modified { get; set; } public void ListPicker\_TextChanged(object sender, EventArgs args) { Modified = true; OnModifiedChanged(); } protected void OnModifiedChanged() { if (ModifiedChanged != null) { ModifiedChanged(this, null); } } }
}
The Modified property works just as I wanted it to, and the ModifiedChanged event fires when it is supposed to, but when I use this code (where ctl is a reference to the ListPicker control)
var x = ctl.GetType().GetEvents();
var modifiedChangedEventInfo = ctl.GetType().GetEvent("ModifiedChanged");to look for the events on the ListPicker, it does not show the ModifiedChanged event. All the other DomainUpDown event show up in x, but not the ModifiedChanged event and modifiedChangedEventInfo is null Any ideas? ... and many thanks
Clive Pottinger Victoria, BC
-
Hello Gurus. Yes, I've painted myself into yet another corner. It seems to be what I do best, so I guess I should stick with it. This time I am trying use reflection to find an event that I have attached to a class, but reflection is not showing the event. I don't know why. I have created a simple class called ListPicker that is based on the DomainUpDown control. It is supposed to act just like a DomainUpDown control, but it supports the Modified property and the ModifiedChanged events that DomainUpDown does not. Here is the code:
using System;
using System.Windows.Forms;namespace WindowsFormsControlLibrary
{
public partial class ListPicker : DomainUpDown
{
public EventHandler ModifiedChanged;public ListPicker() { InitializeComponent(); // Register our TextChanged hander. TextChanged += new EventHandler(ListPicker\_TextChanged); Modified = false; } public bool Modified { get; set; } public void ListPicker\_TextChanged(object sender, EventArgs args) { Modified = true; OnModifiedChanged(); } protected void OnModifiedChanged() { if (ModifiedChanged != null) { ModifiedChanged(this, null); } } }
}
The Modified property works just as I wanted it to, and the ModifiedChanged event fires when it is supposed to, but when I use this code (where ctl is a reference to the ListPicker control)
var x = ctl.GetType().GetEvents();
var modifiedChangedEventInfo = ctl.GetType().GetEvent("ModifiedChanged");to look for the events on the ListPicker, it does not show the ModifiedChanged event. All the other DomainUpDown event show up in x, but not the ModifiedChanged event and modifiedChangedEventInfo is null Any ideas? ... and many thanks
Clive Pottinger Victoria, BC
Of what type is ctl? Is it of the class DomainUpDown or of ListPicker? If it is of DomainUpDown, you probably only see the events of this class and not of any derived classes.
And from the clouds a mighty voice spoke:
"Smile and be happy, for it could come worse!"And I smiled and was happy
And it came worse. -
Hello Gurus. Yes, I've painted myself into yet another corner. It seems to be what I do best, so I guess I should stick with it. This time I am trying use reflection to find an event that I have attached to a class, but reflection is not showing the event. I don't know why. I have created a simple class called ListPicker that is based on the DomainUpDown control. It is supposed to act just like a DomainUpDown control, but it supports the Modified property and the ModifiedChanged events that DomainUpDown does not. Here is the code:
using System;
using System.Windows.Forms;namespace WindowsFormsControlLibrary
{
public partial class ListPicker : DomainUpDown
{
public EventHandler ModifiedChanged;public ListPicker() { InitializeComponent(); // Register our TextChanged hander. TextChanged += new EventHandler(ListPicker\_TextChanged); Modified = false; } public bool Modified { get; set; } public void ListPicker\_TextChanged(object sender, EventArgs args) { Modified = true; OnModifiedChanged(); } protected void OnModifiedChanged() { if (ModifiedChanged != null) { ModifiedChanged(this, null); } } }
}
The Modified property works just as I wanted it to, and the ModifiedChanged event fires when it is supposed to, but when I use this code (where ctl is a reference to the ListPicker control)
var x = ctl.GetType().GetEvents();
var modifiedChangedEventInfo = ctl.GetType().GetEvent("ModifiedChanged");to look for the events on the ListPicker, it does not show the ModifiedChanged event. All the other DomainUpDown event show up in x, but not the ModifiedChanged event and modifiedChangedEventInfo is null Any ideas? ... and many thanks
Clive Pottinger Victoria, BC
that must be because it isn't an event, it is just a delegate. Add the
event
keyword to your declaration and it should show up. :)Luc Pattyn [My Articles] Nil Volentibus Arduum
-
that must be because it isn't an event, it is just a delegate. Add the
event
keyword to your declaration and it should show up. :)Luc Pattyn [My Articles] Nil Volentibus Arduum
-
that must be because it isn't an event, it is just a delegate. Add the
event
keyword to your declaration and it should show up. :)Luc Pattyn [My Articles] Nil Volentibus Arduum
Whoa! :doh: I feel like I've just gone to the hospital to ask a surgeon why I keep getting headaches whenever I hit my head with a hammer. There should be a facepalm emoticon. Thanks guys. I'll apply your suggestion to the code tonight and see what happens.
Clive Pottinger Victoria, BC
-
Whoa! :doh: I feel like I've just gone to the hospital to ask a surgeon why I keep getting headaches whenever I hit my head with a hammer. There should be a facepalm emoticon. Thanks guys. I'll apply your suggestion to the code tonight and see what happens.
Clive Pottinger Victoria, BC
Yup - that was it. embarassing... that's all I have to say... embarassing oh, and thanks. BTW: I find it interesting that the question from one C.D.P was answered by another C.D.P.
Clive Pottinger Victoria, BC
-
Yup - that was it. embarassing... that's all I have to say... embarassing oh, and thanks. BTW: I find it interesting that the question from one C.D.P was answered by another C.D.P.
Clive Pottinger Victoria, BC
Don't worry. And you're welcome. BTW: you shouldn't reply to self, as nobody gets mail notification when you do. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
Yup - that was it. embarassing... that's all I have to say... embarassing oh, and thanks. BTW: I find it interesting that the question from one C.D.P was answered by another C.D.P.
Clive Pottinger Victoria, BC
Clive D. Pottinger wrote:
BTW: I find it interesting that the question from one C.D.P was answered by another C.D.P
In my case the 'CDP' has nothing to do with my name or initials. A CDP1802 or RCA1802[^] was the CPU in my first computer. Look closely at the assembly code sample. One of the instructions, the one to designate the stack pointer, still does not fail to bring a wide grin to programming.
And from the clouds a mighty voice spoke:
"Smile and be happy, for it could come worse!"And I smiled and was happy
And it came worse. -
Clive D. Pottinger wrote:
BTW: I find it interesting that the question from one C.D.P was answered by another C.D.P
In my case the 'CDP' has nothing to do with my name or initials. A CDP1802 or RCA1802[^] was the CPU in my first computer. Look closely at the assembly code sample. One of the instructions, the one to designate the stack pointer, still does not fail to bring a wide grin to programming.
And from the clouds a mighty voice spoke:
"Smile and be happy, for it could come worse!"And I smiled and was happy
And it came worse.An instruction any man would love to comply with I think :laugh:
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
-
An instruction any man would love to comply with I think :laugh:
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
-
that must be because it isn't an event, it is just a delegate. Add the
event
keyword to your declaration and it should show up. :)Luc Pattyn [My Articles] Nil Volentibus Arduum