问题
Playing with new RTTI module, I couldn't find a way to set an event handler with the new utilities. Trying something like this:
LProp := TRttiContext.Create.GetType(Form1.ClassInfo).AsInstance.GetProperty('OnClick');
LProp.SetValue(Form1, {a TValue!});
SetValue needs a TValue passed but I've yet to find a way to represent a TMethod via a TValue. Any comments?
ps. And a side question.. It seems prefixing RTTI variables with "L" is a convention (and a good one IMO, meta code turns into a puzzle real quick). But what does it actually stand for?
回答1:
You can make TValue contain a value of an aribtrary type with the From method. Try something like this:
lValue := TValue.From<TNotifyEvent>(eventHandler);
BTW I think the L just means a Local variable.
来源:https://stackoverflow.com/questions/1482461/how-to-set-event-handlers-via-new-rtti-d2010