首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

关于反射方法的调用,

2013-09-07 
关于反射方法的调用,救命啊。工作需要,用到winform的PropertyGrid,要在下面的红色框的位置加鼠标事件,用正

关于反射方法的调用,救命啊。
工作需要,用到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.");
                        });
                }
            }

但这个在红框左侧那一栏响应,红框所在的一列还是无法响应。
通过反编译查到PropertyGrid中的gridView中的selectedGridEntry是显示Name和Jack的控件,但是这个控件没有可以直接注册的鼠标事件。而是自身调用两个方法注册的事件,我想注册的话就要调用这两个方法。两个方法代码如下。
        
        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:;
            }
        }


现在可以反射出这两个方法,但是调用的时候第一个参数(???)需要传一个方法所在类的实例,但是那个实例是反射出来的,只有FieldInfo,并没有真正的实例,怎么办?代码如下。
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[] { });
                }
            }
        }
    }


[解决办法]
你是要给jack那里赋值?jack那里是个什么东西?
[解决办法]
你实例化一个PropertyGrid传进去不就行了
[解决办法]
你应该就是想写个继承自PropertyGrid自定义控件然后添加内容吧?
???写成this就行了

热点排行