关于反射方法的调用,救命啊。
工作需要,用到winform的PropertyGrid,要在下面的红色框的位置加鼠标事件,用正常PropertyGrid自带的鼠标事件完全没有响应,捕获不到.
通过继承PropertyGrid在构造是可以注册鼠标事件。
foreach (Control control in this.Controls)
{
Type controlType = control.GetType();
if (controlType.Name == "PropertyGridView")
{
control.MouseDoubleClick += ((object sender, MouseEventArgs e) =>
{
MessageBox.Show("Hello.");
});
}
}
public virtual void AddOnValueDoubleClick(EventHandler h)
{
this.AddEventHandler(EVENT_VALUE_DBLCLICK, h);
}
protected virtual void AddEventHandler(object key, Delegate handler)
{
lock (this)
{
if (handler != null)
{
for (EventEntry entry = this.eventList; entry != null; entry = entry.next)
{
if (entry.key == key)
{
entry.handler = Delegate.Combine(entry.handler, handler);
goto Label_0059;
}
}
this.eventList = new EventEntry(this.eventList, key, handler);
}
Label_0059:;
}
}
class CustomPropertyGrid : System.Windows.Forms.PropertyGrid
{
private Type propertyGridViewType = null;
public CustomPropertyGrid()
{
foreach (Control control in this.Controls)
{
Type controlType = control.GetType();
if (controlType.Name == "PropertyGridView")
{
propertyGridViewType = controlType;
FieldInfo fi = controlType.GetField("selectedGridEntry", BindingFlags.NonPublic | BindingFlags.Instance);
MethodInfo mi = fi.FieldType.GetMethod("AddEventHandler", BindingFlags.NonPublic | BindingFlags.Instance);
mi.Invoke(???, new object[] { });
}
}
}
}