Anyone know how to use TValue.AsType<TNotifyEvent> properly?
- by Mason Wheeler
I'm trying to use RTTI to add an event handler to a control, that may already have an event handler set. The code looks something like this:
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;
I want this so I can do this in MyOnChange:
begin
if assigned(FOldOnChange) then
FOldOnChange(Sender);
//additional code here
end;
Unfortunately, the compiler doesn't seem to like the line FOldOnChange := val.AsType<TNotifyEvent>;. It says
E2010 Incompatible types: 'procedure,
untyped pointer or untyped parameter'
and 'TNotifyEvent'
Anyone know why that is or how to fix it? It looks right to me...