'System.EventHandler'到'DevExpress.XtraEditors.Controls.ChangingEventHandler'转换错误

时间:2015-11-25 15:11:24

标签: c# eventhandler

    private void RegisterObjectEvents(XElement xelement, XtraForm form)
    {
        XElement fEventElement = xelement.Element("ObjectEvents");
        if (fEventElement != null)
        {
            var fSubElement = fEventElement.Elements();
            foreach (var item in fSubElement)
            {
                if (!IsformEvent(item.Name.ToString()))
                {
                    Control control = null;
                    for (int i = 0; i <= fObjectList.Count - 1; i++)
                    {
                        if (fObjectList[i].Name == GetObjectNameFromString(item.Name.ToString()))
                        {
                            control = fObjectList[i];
                            break;
                        }
                    }
                    if (control != null)
                    {
                        EventInfo ei = control.GetType().GetEvent(GetEventNameFromString(item.Name.ToString()));                            
                        EventHandler del = (o, args) => ExecuteLua(item.Value, o, args);                            
                        ei.AddEventHandler(control,del );//getting exception here
                    }
                }
            }
        }
    }

    public void ExecuteLua(string luastring,object sender , EventArgs e)
    {
        try
        {              
            Lua.LoadCLRPackage();
            Lua["sender"] = (Control)sender;
            Lua["eventarg"] = e;
            Lua["viewer"] = this;
            Lua["form"] = this.WinForm;                
            Lua.DoString(luastring);               
        }
        catch (Exception exp)
        {
            MessageBox.Show(exp.Message.ToString());
        }
    }

我正在尝试从Xelement读取Control对象和事件信息,并注册控件的事件运行时。但是得到

  

'System.EventHandler'到DevExpress.XtraEditors.Controls.ChangingEventHandler'转换异常。

我该如何解决?

1 个答案:

答案 0 :(得分:0)

我找到了这样的方式;

        public static Delegate ConvertDelegate(Delegate originalDelegate, Type targetDelegateType)
    {
        return Delegate.CreateDelegate(
            targetDelegateType,
            originalDelegate.Target,
            originalDelegate.Method) ;
    }   

我正在使用它;

private void RegisterObjectEvents(XElement xelement, XtraForm form)
    {             
        XElement fEventElement = xelement.Element("ObjectEvents");
        if (fEventElement != null)
        {
            var fSubElement = fEventElement.Elements();
            foreach (var item in fSubElement)
            {
                if (!IsformEvent(item.Name.ToString()))
                {
                    Control control = null;
                    for (int i = 0; i <= fObjectList.Count - 1; i++)
                    {
                        if (fObjectList[i].Name == GetObjectNameFromString(item.Name.ToString()))
                        {
                            control = fObjectList[i];
                            break;
                        }
                    }
                    if (control != null)
                    {
                        EventInfo ei = control.GetType().GetEvent(GetEventNameFromString(item.Name.ToString()));
                        Type tDelegate = ei.EventHandlerType;                           
                        EventHandler del = (o, args) => ExecuteLua(item.Value,o, args);
                        ei.AddEventHandler(control, ConvertDelegate(del, tDelegate));                                  
                    }
                }
            }
        }
    }