Re: How to Verify if EventHandler for ValueChanged has been set?
- From: <yuriylsh@xxxxxxxxxxx>
- Date: Tue, 16 Dec 2008 14:04:20 -0600
If you still going to use reflection, you can use something like that:
private bool AreListenersAdded(TextBox control, string eventName)
{
var controlType = control.GetType();
var fields = controlType.GetFields(BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
var eventField = fields.SingleOrDefault(f => f.Name == "Event" + eventName);
if (eventField != null)
{
var eventsProperty = (typeof(Control)).GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);
var events = eventsProperty.GetValue(control, null) as EventHandlerList;
if (events != null)
{
var textChangedEvent = eventField.GetValue(control);
var eventDelegate = events[textChangedEvent];
if (eventDelegate != null)
return eventDelegate.GetInvocationList().Length > 0;
}
}
return false;
}
<richard.martino@xxxxxxxxxxxxxxxx> wrote in message news:6b9b3795-ad42-4460-b598-17582f09dbd2@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Dec 16, 1:12 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
Richard,
You would have to use reflection for this, as you don't have access to
the event handler list from outside the instance that exposes the event.
You would have to look for the backing field for the delegate, but the
thing is, there is no guaranteed linking between that field and the event
exposed. For the standard event implementation (no add/remove handlers), it
should work, but you might run into some custom add/remove handlers and it
won't be that easy to figure out what the backing field for the event is.
Are you doing this for unit testing, or within your app? If you are
doing it within your app, I would recommend making this a unit test or maybe
a custom rule for FxCop.
--
- Nicholas Paldino [.NET/C# MVP]
- m...@xxxxxxxxxxxxxxxxxxxxxxxxxxx
<richard.mart...@xxxxxxxxxxxxxxxx> wrote in message
news:6b0427f8-3b02-406a-af2f-b8145011866f@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
>I have a huge Windows desktop application with over 100 individual
> controls, like TextBox, NumericUpDown, ComboBox, etc.
> I want each one to have its ValueChanged (or TextChanged) event
> handler set, like:
> -----------
> System.Windows.Forms.TextBox cityTextBox;
> cityTextBox = new TextBox();
> cityTextBox.TextChanged += new EventHandler(cityTextBox_TextChanged);
> -----------
> I can loop through all of the controls of the application starting
> with this.Controls and, recursively their children, finding everyone
> of my 100 individual controls.
> I want my software to verify if I have every TextChanged or
> ValueChanged event handler set.
> Any clues?
> Thanks- Hide quoted text -
- Show quoted text -
Nicholas:
Yes, I have tried reflection as follows:
----------
using System.Reflection;
System.Type type = cityTextBox.GetType();
EventInfo eventInfo = type.GetEvent("TextChanged",
BindingFlags.Instance | BindingFlags.Public);
MethodInfo methodInfo = eventInfo.GetRaiseMethod(true);
//
// However, methodInfo always comes back null.
//
----------
Is the above code what you had in mind?
.
- References:
- How to Verify if EventHandler for ValueChanged has been set?
- From: richard . martino
- Re: How to Verify if EventHandler for ValueChanged has been set?
- From: Nicholas Paldino [.NET/C# MVP]
- Re: How to Verify if EventHandler for ValueChanged has been set?
- From: richard . martino
- How to Verify if EventHandler for ValueChanged has been set?
- Prev by Date: Re: Can C# 3.0 not be used in websites?
- Next by Date: Re: UserControl, GUI inside the application on runing time, PlugIn problem
- Previous by thread: Re: How to Verify if EventHandler for ValueChanged has been set?
- Next by thread: Re: How to Verify if EventHandler for ValueChanged has been set?
- Index(es):
Relevant Pages
|