From Social Madrill, 7 Years ago, written in Delphi (Object Pascal).
This paste is a reply to Invokable from Asbjørn
- view diff
Embed
  1. In this method:
  2.  
  3. -------------------------
  4. procedure TWidgetImpl<T>.SetInstance(const Inst: T);
  5. var
  6.   ctx: TRttiContext;
  7.   typ: TRttiType;
  8.   m: TRttiMethod;
  9. begin
  10.   typ := ctx.GetType(TypeInfo(T));
  11.   m := typ.GetMethod('Initialize');
  12.   if (Assigned(m)) then
  13.   begin
  14.     m.Invoke(TValue.From<T>(Inst), []);
  15.   end;
  16. end;
  17. --------------
  18. I have a private var (fSubscriber) of T and I get access like this:
  19.  
  20. typ := ctx.GetType((fSubscriber as TObject).ClassInfo);
  21. ....
  22. m.Invoke((fSubscriber as TObject),[]);
  23.  
  24.  
  25. Your code is different in these two lines. I think I tried to use TypeInfo and didn't work nicely if I recall correctly.
  26. Is it wrong what I am doing?
  27.  
  28. Thanks