|
■No2761 (まきこ さん) に返信
> イメージとしては下記のような処理をしたいと思っています。
> if (this.comboBox1.SelectedIndexChanged == null) {}
一応、こんな感じで判定できましたが……実用性は無いかも。
private void button1_Click(object sender, EventArgs e)
{
Delegate[] methodList = GetCaller(comboBox1);
StringBuilder sb = new StringBuilder("comboBox1 の SelectedIndexChanged イベントは未登録");
if (methodList.Length > 0)
{
sb = new StringBuilder();
foreach (Delegate method in methodList)
{
sb.AppendLine(method.Method.Name);
}
}
MessageBox.Show(sb.ToString());
}
public Delegate[] GetCaller(ComboBox target)
{
Type type = typeof(ComboBox);
BindingFlags all = BindingFlags.Default;
all |= BindingFlags.Public;
all |= BindingFlags.NonPublic;
all |= BindingFlags.Instance;
all |= BindingFlags.IgnoreCase;
all |= BindingFlags.Static;
EventDescriptorCollection events = TypeDescriptor.GetEvents(target);
EventDescriptor myEvent = events.Find("SelectedIndexChanged", false);
MethodInfo mi = type.GetMethod("get_Events", all);
EventHandlerList list = (EventHandlerList)mi.Invoke(target, new object[] { });
FieldInfo fi = type.GetField("EVENT_SELECTEDINDEXCHANGED", all);
object key = fi.GetValue(target);
if (list[key] == null)
{
return new Delegate[0];
}
else
{
return list[key].GetInvocationList();
}
}
|