Skip to content

Instantly share code, notes, and snippets.

@dipold
Created July 18, 2018 18:57
Show Gist options
  • Save dipold/731f6262e208ea65e4b1211795ddea36 to your computer and use it in GitHub Desktop.
Save dipold/731f6262e208ea65e4b1211795ddea36 to your computer and use it in GitHub Desktop.
program Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.Rtti,
System.SysUtils;
type
{$M+}
IFoo = interface
['{65C02E99-4136-4572-B2C7-365AEA174E6E}']
function GetBar: String;
end;
{$M-}
{$RTTI EXPLICIT METHODS([vcPrivate, vcProtected, vcPublic, vcPublished])}
TFoo = class(TInterfacedObject, IFoo)
strict private
function GetBar: String;
end;
function TFoo.GetBar: String;
begin
Result := 'Bar';
end;
var
LMethod: TRttiMethod;
LFoo: IFoo;
begin
LFoo := TFoo.Create;
LMethod := TRttiContext.Create.GetType(TFoo).GetMethod('GetBar');
Writeln(LMethod.Invoke(LFoo as TObject, []).AsString); //Prints: Bar
LMethod := TRttiContext.Create.GetType(TypeInfo(IFoo)).GetMethod('GetBar');
Writeln(LMethod.Invoke(LFoo as TObject, []).AsString); //Access Violation
ReadLn;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment