How to set event handlers via new RTTI? [D2010]

巧了我就是萌 提交于 2021-02-19 03:19:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!