我正在尝试使用RTTI向控件添加事件处理程序,该控件可能已经设置了事件处理程序.代码看起来像这样: var prop: TRttiProperty; val: TValue;begin prop := FContext.GetType(MyControl.ClassInfo).GetProperty('On
var prop: TRttiProperty; val: TValue; begin prop := FContext.GetType(MyControl.ClassInfo).GetProperty('OnChange'); val := prop.GetValue(MyControl); FOldOnChange := val.AsType<TNotifyEvent>; prop.SetValue(MyControl, TValue.From<TNotifyEvent>(self.MyOnChange)); end;
我想要这个,所以我可以在MyOnChange中做到这一点:
begin if assigned(FOldOnChange) then FOldOnChange(Sender); //additional code here end;
不幸的是,编译器似乎不喜欢FOldOnChange:= val.AsType< TNotifyEvent>;行.它说
E2010 Incompatible types: ‘procedure,
untyped pointer or untyped parameter’
and ‘TNotifyEvent’
任何人都知道为什么会这样或如何解决它?看起来对我来说……
FOldOnChange是方法指针类型,而AsType< TNotifyEvent>是一种方法.编译器认为您正在尝试将方法分配给方法指针.解决方案是将append()附加到方法调用以强制它,并使用方法的返回值作为要分配给FOldOnChange的值.这是一个完整的例子:
uses SysUtils, Rtti; type TEv = procedure(Sender: TObject) of object; TObj = class private FEv: TEv; public property Ev: TEv read FEv write FEv; class procedure Meth(Sender: TObject); end; class procedure TObj.Meth(Sender: TObject); begin end; procedure P; var ctx: TRttiContext; t: TRttiType; p: TRttiProperty; v: TValue; o: TObj; e: TEv; begin t := ctx.GetType(TObj); p := t.GetProperty('Ev'); o := TObj.Create; try // Set value explicitly o.Ev := TObj.Meth; // Get value via RTTI v := p.GetValue(o); //e := v.AsType<TEv>; // doesn't work e := v.AsType<TEv>(); // works finally o.Free; end; end; begin try P; except on e: Exception do Writeln(e.Message); end; end.