title: How do I use a string in TRttiMethod.Invoke as parameter properly?
body: <p>I'm trying to generalize the content validation of visual components with the Text-property using RTTI but when I try to pass a string value into TRttiMethod.Invoke, I get the Message "Invalid Typecast". (Actually "Ungültige Typumwandlung" but I guess, that was a fitting translation.)</p>
  
  <p>The code below is stripped of all security measures, assertions and so on, assuming all passed objects are just perfect.</p>
  
  <pre><code>procedure ValidateTextFieldAndSetFocus(const Field: TObject; const Validator: TObject; const errorStates: array of TStringValidationResult; const sErrorMessage: string);
  var
    context  : TRttiContext;
    objField : TRttiType;
    objValid : TRttiType;
    prop     : TRttiProperty;
    execute  : TRttiMethod;
    I        : Integer;
    validResult : TStringValidationResult;
    value    : TValue;
  begin
    context  := TRttiContext.Create;
    objField := context.GetType(Field.ClassInfo);
    objValid := context.GetType(Validator.ClassInfo);
    prop     := objField.GetProperty('Text');
    value    := prop.GetValue(Field);
    execute  := objValid.GetMethod('Execute');
    for I := 0 to High(errorStates) do
      if execute.Invoke(Validator,[value]).TryAsType&lt;TStringValidationResult&gt;(validResult) then
        if validResult = errorStates[I] then
        begin
          SetFocusIfCan(Field);
          raise Exception.Create(sErrorMessage);
        end;
  end;
  </code></pre>
  
  <p>The Validator's Execute only has one string-Parameter. I've seen examples where strings were passed directly into the array of TValue, but then I get the same typecast error.</p>
  
  <p>edit: </p>
  
  <p>The actual error appears in <code>execute.Invoke(Validator,[value])</code>. </p>
  
  <p>Example</p>
  
  <pre><code>TNoSemicolonNullValidator = class
    class function Execute(const aStr: string): TStringValidationResult;
  end;
  
  procedure TestValidation;
  var
    Validator : TNoSemicolonNullValidator;
  begin
    Validator := TNoSemicolonNullValidator.Create;
    try
      ValidateTextFieldAndSetFocus(Edit1,Validator,[svInvalid],'Edit1 is invalid!');
    finally
      Validator.Free;
    end;
  end;
  </code></pre>