“System.EventHandler”到“DevExpress.XtraEditors.Controls.Chang

本文关键字:XtraEditors Controls Chang DevExpress System EventHandler | 更新日期: 2023-09-27 18:24:14

    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 读取控件对象和事件信息并注册控件的事件运行时。但是得到

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

我该如何解决?

“System.EventHandler”到“DevExpress.XtraEditors.Controls.Chang

我找到了这样的方法;

        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));                                  
                    }
                }
            }
        }
    }