title: FreePascal RTTI. Is there a way to invoke method?
body: <p>I'm trying to find out if there is a way to do things similar to Delphi's enhanced RTTI features.</p>
  
  <p>As far as I know FPC doesn't provide RTTI features which appeared in Delphi since Delphi 2010. But I'd like to find some way to do a few tricks during runtime.</p>
  
  <p>Using <code>typinfo</code> unit in FPC I can do such things as:</p>
  
  <ul>
  <li>get Object published property list - via <code>getPropList</code> from <code>typinfo</code> unit;</li>
  <li>get/set value of the Object's published property - via <code>GetPropValue(...): Variant</code>/<code>SetPropValue(...Value: Variant)</code>;</li>
  <li>get published method - via <code>MethodAddres</code>;</li>
  </ul>
  
  <p><strong>But I haven't found a way to do things like</strong>:</p>
  
  <ul>
  <li>call methods;</li>
  <li>call constructors, or create Objects;</li>
  </ul>
  
  <p>Update: the problem with constructors is much like methods one - I want to have a way to pass different params in it:</p>
  
  <pre><code>// concept of code
  type
  
  TClass = class of TObject;
  
  TMyClass1 = class
    public
      constructor Create(Param1: Integer; Param2: string); override;
    end;
  
  TMyClass2 = class
    public
      constructor Create(ObjectParam: Object); override;
    end;
  
  TParams = array of Variant;
  
  var 
  Classes: array of TClass 
  Instances: array of Object;
  ParamArray: array of TParams;
  
  ... 
  
  For I := 0 to Count-1 do 
  begin
    LocalConstructor := @(Classes[I].Create);
    Instances[I] := CallConstructor(LocalConstructor, ParamArray[I]);
  end;
  </code></pre>
  
  <p>So I need to call constructor without knowing its signature.</p>
  
  <p>So my problem is to call an Object's method and pass some parameters to it. It could look like <code>function CallMethod(Instance: Object; MethodName: String; Params: array of Variant): Variant;</code></p>
  
  <p>If I'm not mistaken it could be solved via Delphi's 2010+ RTTI. But before using enhanced Delphi's RTTI I'd like to understand is it possible in FPC.</p>
  
  <p>In other words my current problem is pass arguments to a routine.
  I know it can be done using this scheme: </p>
  
  <pre><code>type
    TmyProc = procedure CallMe(x: byte);
  ...
    var proc: TmyProc;
  ...
    proc := pointerToFunc^;
    proc(0);
  </code></pre>
  
  <p>But I need to implement it without knowing count and types of parameters (during compile time).</p>
  
  <p>There are a few links related to the topic:</p>
  
  <p><a href="https://stackoverflow.com/questions/4186458/delphi-call-a-function-whose-name-is-stored-in-a-string">Delphi: Call a function whose name is stored in a string</a></p>
  
  <p><a href="http://www.swissdelphicenter.ch/torry/showcode.php?id=1745" rel="nofollow noreferrer">http://www.swissdelphicenter.ch/torry/showcode.php?id=1745</a> </p>
  
  <p>The second article (<a href="http://www.swissdelphicenter.ch/torry/showcode.php?id=1745" rel="nofollow noreferrer">http://www.swissdelphicenter.ch/torry/showcode.php?id=1745</a>) describes a way to pass arguments to a routine imported from DLL by name. Which is almost that I need I suppose. But I'm not sure that way is reliable. </p>
  
  <p>Maybe there's any library, which implements these things using "old" typinfo unit (without RTTI unit)?</p>
  
  <p>Also I'm interested in a way to create some kind of universal event handlers - procedures which can be assigned to different events (with different sets of parameters) e.g.:</p>
  
  <pre><code>procedure myEventHandler(params: array of variant);
  ...
  Button.OnClick := myEventHandler;
  Button.OnMouseMove := myEventHandler;
  </code></pre>
  
  <p>is this possible? Or at least something similar to it?</p>